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.
PHP Switch Function
Started by Imque, May 15 2013 07:11 AM
2 replies to this topic
#1
Posted 15 May 2013 - 07:11 AM
#2
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:
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
Posted 15 May 2013 - 08:20 AM
Ok, thanks. Huge help.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











