Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: ?action= help?

  1. #1
    AJM
    AJM is offline
    New Coder
    Join Date
    Nov 2011
    Location
    USA
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post

    ?action= help?

    Hi all,

    I am fairly new PHP (about a years experience). I was just wondering if someone could explain to me how I would be able to make, for example, index.php?action=post show different code than index.php.


    Thanks,

    -AJM

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That would be your querystring. Anything provided in it can be retrieved from the $_GET superglobal.
    PHP Code:
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    switch (
    $action)
    {
        case 
    'post':
            print 
    'Action "post" provided';
            break;
        default:
            print 
    'No valid action provided.';

    For example.

  • #3
    AJM
    AJM is offline
    New Coder
    Join Date
    Nov 2011
    Location
    USA
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Many thanks to you Fou-Lu. Hopefully I can implement this into my pages.

    Thanks again,

    -AJM

  • #4
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    There is a few methods to use, here is the one I use.

    PHP Code:
    if (isset($_GET["x"])) {
        
    $x explode(":",$_GET["x"]);

        switch(
    $x[0])
        {
            case 
    'deposit':
                
    deposit();
            break;

                    case 
    'withdraw':
                
    withdraw();
            break;
            

     }
    }
    else {
      
    bank();

    So if you do bank.php it automatically loads the bank() function.
    or if you want something else it'd be like bank?x=deposit
    If you don't want to auto load a function, remove the else statement.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can skip the if/else completely. If you default the assignment to a non-viable option such as an empty string, you can use the switch and provide it with the default case which executes when no other options are available to use.

  • #6
    AJM
    AJM is offline
    New Coder
    Join Date
    Nov 2011
    Location
    USA
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    I am using cookies with this for an admin manage page/script so I AM using if statements. My code looks a bit like this:
    PHP Code:
    if(isset($_COOKIE['cookie']) && $_GET['id'] == "1")
      print 
    "Page Stuff";
    else 
      print 
    ""

  • #7
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by AJM View Post
    I am using cookies with this for an admin manage page/script so I AM using if statements. My code looks a bit like this:
    PHP Code:
    if(isset($_COOKIE['cookie']) && $_GET['id'] == "1")
      print 
    "Page Stuff";
    else 
      print 
    ""
    I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

    So to optimise the speed i would use echo and not print

  • #8
    AJM
    AJM is offline
    New Coder
    Join Date
    Nov 2011
    Location
    USA
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by devinmaking View Post
    I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

    So to optimise the speed i would use echo and not print
    Thanks for the advice man... I'll try it out.

    -AJM

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by devinmaking View Post
    I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

    So to optimise the speed i would use echo and not print
    This is a loaded benchmark that is difficult to determine. The variable evaluation does tend to be faster in echo while using a concat instead of internal expansion. Printf with evaluation I'd expect to beat both of these.
    I can manufacture a benchmark which supports probably +/- upwards of 200% or so off of each, but one thing to mention is that we are talking 2 or 3 nano-seconds difference. That's peanuts. You will find that print will more consistently beat echo in constant data, while echo tends to beat print in concatenated data (which is why I'd expect printf to defeat both, though I'm not sure how that would related with constant data [which given its origin will likely beat both in both constant and evaluated data]).
    For what you'd save, I'd not recommend switching between the two. I'd suggest choosing one and sticking with it for consistency.

  • Users who have thanked Fou-Lu for this post:

    devinmaking (11-27-2011)

  • #10
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Fou-Lu View Post
    This is a loaded benchmark that is difficult to determine. The variable evaluation does tend to be faster in echo while using a concat instead of internal expansion. Printf with evaluation I'd expect to beat both of these.
    I can manufacture a benchmark which supports probably +/- upwards of 200% or so off of each, but one thing to mention is that we are talking 2 or 3 nano-seconds difference. That's peanuts. You will find that print will more consistently beat echo in constant data, while echo tends to beat print in concatenated data (which is why I'd expect printf to defeat both, though I'm not sure how that would related with constant data [which given its origin will likely beat both in both constant and evaluated data]).
    For what you'd save, I'd not recommend switching between the two. I'd suggest choosing one and sticking with it for consistency.
    I did not know this, thanks for the heads up


  •  

    Tags for this Thread

    Posting Permissions

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