This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Imque's profile picture

PHP Switch Function

Started by Imque, 15 May 2013 - 06:11 AM
Imque #1
Posted 15 May 2013 - 08: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.
SadKingBilly #2
Posted 15 May 2013 - 08: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
Imque #3
Posted 15 May 2013 - 09:20 AM
Ok, thanks. Huge help. :D/>