+ Reply to Thread
Results 1 to 8 of 8

Thread: Dynamic Switch?

  1. #1
    Vouksh is offline Smart College Student Vouksh is on a distinguished road
    Join Date
    Jan 2005
    Location
    Ohio
    Age
    24
    Posts
    202
    Rep Power
    9

    Question Dynamic Switch?

    Well, I was wondering, is it possible to have a "dynamic" switch statement?
    kinda like...
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    switch(
    strtolower($_GET['section']))
    {
        foreach(
    $allowed as $rd)
        {
            case 
    $rd:
                include 
    $rd.'.inc.php';
            break;
        }
    }
    ?>
    (this doesn't work btw)
    I guess there's always the possibility of writing it to the included file, but that' d be a pain in the butt, but it'd work, no?

    Gimme your thoughts on this.

  2. #2
    unclekyky's Avatar
    unclekyky is offline Jovially Avuncular unclekyky is on a distinguished road
    Join Date
    Sep 2004
    Age
    23
    Posts
    5,184
    Rep Power
    14

    Re: Dynamic Switch?

    Just use an if statement inside the foreach.
    Spore-Game - The Ultimate Spore Fan-Site
    Abnegating Avunculicide Since 1601 | YC Wiki - "Quidquid latine dictum sit, altum sonatur."

  3. #3
    abdussamad is offline A Toddler - Don't be Fooled! abdussamad is on a distinguished road
    Join Date
    Oct 2005
    Posts
    18
    Rep Power
    0

    Re: Dynamic Switch?

    Nice idea. Try this code:

    Code:
    <?php
    $allowed = array('codedb', 'members', 'forums', 'content');
    if(!empty($_GET['section']))
    {
       $rd=strtolower($_GET['section']);
    
       if(in_array($rd,$allowed)) 
                include $rd.'.inc.php';
    }
    ?>
    Or if you prefer using a switch statement:

    Code:
    <?php
    $allowed = array('codedb', 'members', 'forums', 'content');
    if(!empty($_GET['section']))
    {
        foreach($allowed as $rd)
        {
            switch(strtolower($_GET['section']))
             {  
                case $rd: include $rd.'.inc.php'; break;
             }  
        }
    }
    ?>

  4. #4
    wGEric is offline New Born wGEric is on a distinguished road
    Join Date
    Dec 2005
    Posts
    2
    Rep Power
    0

    Re: Dynamic Switch?

    The first bit of code that bdussamad posted is the best way to do it. You shouldn't loop if you don't have to.

  5. #5
    Mau
    Mau is offline Banned Mau is on a distinguished road
    Join Date
    Jun 2005
    Location
    California, USA
    Age
    23
    Posts
    2,821
    Rep Power
    0

    Re: Dynamic Switch?

    Honestly, this code is very ineffecient:
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    if(!empty(
    $_GET['section']))
    {
        foreach(
    $allowed as $rd)
        {
            switch(
    strtolower($_GET['section']))
             {  
                case 
    $rd: include $rd.'.inc.php'; break;
             }  
        }
    }
    ?>
    The same thing is accomplished much better by using the first bit of code posted above.

  6. #6
    IntellEJent's Avatar
    IntellEJent is offline Active Supporter IntellEJent is on a distinguished road
    Join Date
    Jan 2005
    Location
    The restaurant at the end of the universe
    Age
    18
    Posts
    1,510
    Rep Power
    10

    Re: Dynamic Switch?

    In_array() is very inefficient, as Mau said. A switch statement is unreasonable if there's only one case, so therefore, I'd much prefer an if statement for this problem, or an altogether different solution altogether. May I also note that this code is vulnerable to register_globals injection? Anyways, here's what I came up with that's dissimilar to any of the others, but sadly is still vulnerable to register_globals injection:
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    $iffed false;
    $get_section strtolower($_GET['section']);
    foreach(
    $allowed as $rd)
    {
        if(
    $get_section $rd)
        {
            
    $iffed $rd;
            include 
    $rd ".inc.php";
        }
    }
    if(
    $iffed === false)
    {
        die( 
    "You or your browser has inputted an invalid page!" );
    }
    ?>

  7. #7
    abdussamad is offline A Toddler - Don't be Fooled! abdussamad is on a distinguished road
    Join Date
    Oct 2005
    Posts
    18
    Rep Power
    0

    Re: Dynamic Switch?

    Intellejent, Mau has in fact pointed out that the in_array() based code that I posted is the better solution. in_array() is a built in php function and is likely optimised by more experienced programmers than you or I (programmers that built the PHP language itself). Hence in_array() is the most efficient.

  8. #8
    Mau
    Mau is offline Banned Mau is on a distinguished road
    Join Date
    Jun 2005
    Location
    California, USA
    Age
    23
    Posts
    2,821
    Rep Power
    0

    Re: Dynamic Switch?

    Additionally, Intellejent, your code just duplicates in_array()--except once it finds a match, it doesn't stop searching. Even if it did, in_array() would be a better choice because it's built in a lower programming level and should execute a tiny bit faster.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Dynamic Signature :: How do you?
    By tomcromp in forum PHP Scripting
    Replies: 5
    Last Post: 10-18-2005, 11:33 AM
  2. Cannot switch desktops (BCPPB 5.0)
    By MiraX33 in forum C and C++ Programming
    Replies: 3
    Last Post: 08-09-2005, 07:04 PM
  3. Static vs. Dynamic
    By dkrrunner in forum C and C++ Programming
    Replies: 1
    Last Post: 07-29-2005, 12:13 PM
  4. Making dynamic urls search engine friendly
    By Vio-Bear in forum Articles, Tutorials, and Guides
    Replies: 0
    Last Post: 06-09-2005, 01:14 PM
  5. Dynamic Clock
    By Aeris7282 in forum Javascript Articles
    Replies: 0
    Last Post: 01-30-2005, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts