SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: What to do?

  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What to do?

    Hey all!

    I'm an experienced Perl programmer beginning to make my way into the PHP world. I realize that there are differences in standards between the two languages, so I've decided to ask you guys and gals for a little help.

    When using Perl, I always used a hash table and went to the proper function based on the incoming URL "action." How is this normally done in PHP?

    Any explaination and examples would be greatly appreciated. Thanks for your time!

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Welcome to PHP. This isn't difficult at all - very easy in fact 8)

    Whenever you need to read in variables to PHP from the URL you use this:

    PHP Code:
    # sample URL -> [url=http://www.mydomain.com?id=10&action=ViewUsers]www.mydomain.com?id=10&action=ViewUsers[/url]
    .
    .
    $Id = (int) $_GET['id'];
    $Action= (string) $_GET['action'];
    .
    .
    # rest of PHP script 
    You may also notice both (int) and (string) ?

    These placed in front of a variable will check that the data is of the correct type before assigning $Id and $Action the contents of $_GET[].

    For future reference you'll also see a lot about $_POST[] ?

    This is the same as $_GET[] but instead of variables passed by the URL, they're passed via a FORM when it's method = 'post'.

    Your going to dig PHP ain't you ? LoL

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry. I guess I wasn't that clear. I understand getting the variables. That's no problem. What I am asking is what is the standard for decided what to do after you get the incoming variables.

    Thanks for the quick reply!

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To execute a function based on $Action ? I use the following:

    PHP Code:
    if(strtolower($Action) == (string) 'newuser') { NewUser(); }
    else if(
    strtolower($Action) == (string) 'adduser') { AddNewUser(); }
    else {
    ManageUsers();

    I suppose this is about the equivelant of your hash table from perl ? PHP doesn't have one that I'm aware of but if/else if/else conditioning will acheive the same effect.

    Or you could use SWITCH - found in the PHP manual at www.php.net.

    BTW I lowercase the $Action in the event someone changes the case of the action in the URL. For example

    PHP Code:
    ... ?action=NEwusER 
    would return false so you put it all to lowercase thus voiding someone mucking your procedures up.

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright. Can anyone else confirm this as a standard? I know that in Perl, this method was somewhat shunned because it's kind of slow in access time (it has to execute every statement until it finds the correct one so that it can continue.

    PHP does have hashes. A hash is basically just an array with an index that does not have to be an integer.

  6. #6
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    That is pretty much up to you. You can handle the variables any way you want with if...else statements, etc.
    I do not think speed is a real issue with the above code as it checks two values and that is it. With more values you may run across some speed issues but you shouldn't be checking a ton of values at the same time....

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright Hartmann. So if I went ahead and used my hash method (keys matched with referenced functions), it'd be acceptable?

    Thanks for the time guys!


Bookmarks

Posting Permissions

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