Jump to content




PHP Switch Function


  • You cannot reply to this topic
2 replies to this topic

#1 Imque

  • Members
  • 134 posts

Posted 15 May 2013 - 07:11 AM

Hi, I am having trouble understanding the PHP Switch function. Can someone please give an example of them and a purpose. Google isnt helping me for this so please no lmgtfy crap.

#2 SadKingBilly

  • Members
  • 160 posts

Posted 15 May 2013 - 07:39 AM

I won't link you to Google, but I will link you to one of the best sources for any information about PHP: the documentation.

My question is, are you having trouble understanding what the switch function does, why it's useful, or how to use it?

Say you've got a variable, $randomvariable, and you want to run a particular block of code depending on the value of the variable, but you don't want to use if-elseif-else. That's where switch statements come in handy:
switch ($randomvariable) {
   case "hello":
	 echo "Random variable says hello.";
	 break;
   case "goodbye":
	 echo "Random variable says goodbye.";
	 break;
   case "whatever":
	 echo "Random variable says whatever.";
	 break;
}
That's the same as doing the following in Lua:
if randomvariable == "hello" then
   print("Random variable says hello.")
elseif randomvariable == "goodbye" then
   print("Random variable says goodbye.")
elseif randomvariable == "whatever" then
   print("Random variable says whatever.")
end


#3 Imque

  • Members
  • 134 posts

Posted 15 May 2013 - 08:20 AM

Ok, thanks. Huge help. :D





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users