Jump to content
The Uniform Server Community

Recommended Posts

Posted

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 :)

IPB Image

 

IPB Image

if you like this Uniform Server User Sing take em i do not mind share

IPB Image

 

IPB Image

Posted

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 best

Ric :)

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...