override Posted June 24, 2008 Report Posted June 24, 2008 how to include a switch statement say i have a code name test1.php <?php switch ($i): case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; default: echo "i is not equal to 0, 1 or 2"; endswitch; ?> and another one name test.php <?php include ("test1.php?i=1"); ?> <?php include ("test1.php?i=0"); ?> would it work if not what would work i have serched all over but no help to be found Quote if you like this Uniform Server User Sing take em i do not mind share
Ric Posted June 25, 2008 Report Posted June 25, 2008 The switch is a block statement; hence pop it in curly braces.The last two lines look wrong! Format takes the following form: expression ? true part : false part; Hence you would have something similar to this for your includes:($i=="5")? include "mpg1.php": include "mpg2.php"; Working example you could try: Save to www as test.php <?php $i=2; switch ($i){ case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; default: echo "i is not equal to 0, 1 or 2"; } ($i=="5")? include "mpg1.php": include "mpg2.php"; ?> Save to www as mpg1.php <? print "<br>"; print "This is the true part"; ?> Save to www as mpg2.php <? print "<br>"; print "This is the false part"; ?> Change $i=5; All the bestRic Quote
jacob lee Posted July 1, 2008 Report Posted July 1, 2008 RIC: the grammar using endswitch is right. override: I think you will solve this with this code: $i = $_GET['i']; switch($i).... then if you use "include(test1.php?i=1);" in your test.php then you will see "i equals 1" as a result. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.