+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP Switch Functions.

  1. #1
    Wizet's Avatar
    Wizet is offline x10 Elder Wizet is an unknown quantity at this point
    Join Date
    May 2008
    Location
    Brooklyn, New York.
    Posts
    644

    PHP Switch Functions.

    Can anyone give me a snippet of a php code for switch functions. I want to make a url so that it appears like ?action=support or ?index=lalala or something like that. I don't want a template that says 0 equals 1 or 1 equals with something like that (I seriously forgot what that switch function template looked like.). If so please include the mention of php files in your code snippet. *Thanks* xD

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: PHP Switch Functions.

    You mean like this:
    PHP Code:
    switch($condition)
    {
        case 
    'value':
            
    //case code
            
    break;
        
        default:
            
    //default code
            
    break;

    Last edited by lemon-tree; 05-15-2010 at 07:40 PM.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Switch Functions.

    Note that switch is a statement, not a function. It's an important difference because, as special forms, control structures can do things that functions can't. As for examples, the manual page has plenty. If you ever have questions about any language, check the official documentation first. That's why it's there.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    Wizet's Avatar
    Wizet is offline x10 Elder Wizet is an unknown quantity at this point
    Join Date
    May 2008
    Location
    Brooklyn, New York.
    Posts
    644

    Re: PHP Switch Functions.

    And what would I have to change for the php file to be recognized and what do i change to make it ?action=lala ?

  5. #5
    brutetal is offline x10Hosting Member brutetal is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    23

    Re: PHP Switch Functions.

    PHP Code:
    $action $_GET['action']//Gets the url request
    echo $ction//Output value 
    example url: index.php?action=printme

    This should help you.

    To prevent sql injections:
    PHP Code:
    $action $_GET['action']//Gets the url request
    $strip_action mysql_real_escape_string($action); //If your using it for querying into mysql db, its highly suggested you do this to prevent sql injections.
    echo $strip_action//Output value 
    Last edited by brutetal; 05-15-2010 at 10:28 PM.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Switch Functions.

    The query string is best accessed via the _GET superglobal. Names and values from the query string become names and values in the array. Read the manual for more information.

    Just make sure to sanitize the input to prevent an injection attack.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    zegnhabi's Avatar
    zegnhabi is offline x10Hosting Member zegnhabi is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    On The Net
    Posts
    43

    Thumbs up Re: PHP Switch Functions.

    A switch or an associative array is preferable to a sequence of ifs. It's more readable and potentially more performant. Numerous ifs is a minor smell
    PHP Code:
    $pages = array(
        
    'home' => array('title' => 'Home''file' => 'index.html'),
        
    'about' => array('title' => 'About Us'),
        
    'contact' => array('title' => 'Contact Us'),
       ...
        
    'invalid' => array('file' => '404.html')
    );
    function 
    getFile($pages$page) {
        if (isset(
    $pages[$page])) {
            return 
    $pages[$page];
        } else {
            return 
    $page '.html';
        }
    }

    // prevent injection attacks
    if (isset($pages$_REQUEST['page'] ])) {
        
    $page getFile($pages$_REQUEST['page']);
        
    $title $pages[$page]['title'];
         ...
    } else {
        
    // invalid page requested
        
    header('HTTP/1.0 404 Not Found');
        
    $page $pages['invalid']['file'];
    }
    include(
    'header.php');
    include(
    $page);
    include(
    'footer.php'); 
    Yo Soy SQLero, ¿Y Tú?


  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Switch Functions.

    Quote Originally Posted by zegnhabi View Post
    A switch or an associative array ...
    Hmm, why does that post seem so familiar?

    @zegnhabi: give proper credit when you quote something, otherwise it's plagiarism.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP GD Functions
    By purpleflame in forum Free Hosting
    Replies: 1
    Last Post: 12-25-2008, 12:37 AM
  2. Image functions
    By Richard in forum Service Alerts
    Replies: 19
    Last Post: 11-20-2007, 08:26 AM
  3. PHP functions
    By danny-w in forum Free Hosting
    Replies: 1
    Last Post: 10-07-2007, 07:22 AM
  4. SWF Php Functions?
    By josh33 in forum Graphics & Webdesign
    Replies: 0
    Last Post: 02-01-2007, 11:41 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
x10hosting free hosting for the masses
dedicated servers