Jump to content

Switch Statement

- - - - -

  • Please log in to reply
4 replies to this topic

#1
docmonkey

docmonkey

    Newbie

  • Members
  • PipPip
  • 10 posts
I'd like to use a switch statement (or something better if anyone has some advice) to run a bit of code if i'm on one page, and different code if i'm on another. What is the best way to do this? Looking for best practices here...

Edited by docmonkey, 12 June 2011 - 11:17 AM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
It varies greatly on what the contents of your page are going to be, however you can freely switch on $_GET['page'] if that were so your wish. You should be sure to sanitize the input if you are going to be displaying or working with the GET variable however.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
You can use the $_SERVER['PHP_SELF'] to determine which page you are viewing! Though you get the extension along with it so you may want to substring it and get rid of the extension!

For example:
substr($_SERVER['PHP_SELF'], 1, strpos($_SERVER['PHP_SELF'], ".php") - 1);

will return the name of the file that is viewed!

Hope this helps you :)

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
$_SERVER['PHP_SELF'] is unsafe and should not be relied upon.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, SCRIPT_NAME should be used at the very least - however I see no practical reason for the script to retrieve its name (you name it something specific for a reason, usually.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users