View previous topic :: View next topic |
Author |
Message |
Override -
Joined: 25 Nov 2003 Posts: 124 Location: Virtual Abyss N Area 61
|
Posted: Tue Jun 24, 2008 9:32 pm Post subject: how to include a switch statement |
|
|
how to include a switch statement
say i have a code name test1.php
Code: | <?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
Code: | <?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 |
|
Back to top |
|
|
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
|
Back to top |
|
|
Override -
Joined: 25 Nov 2003 Posts: 124 Location: Virtual Abyss N Area 61
|
Posted: Tue Jun 24, 2008 10:10 pm Post subject: |
|
|
yea but how to include test1.php?i=1 in another script |
|
Back to top |
|
|
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
Posted: Tue Jun 24, 2008 10:14 pm Post subject: |
|
|
Override wrote: | yea but how to include test1.php?i=1 in another script |
Ah sorry I misunderstood the question. You can't do it. Its disallowed the way PHP works. I read a topic about it not long ago but I can't remember where. _________________ Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Tue Jun 24, 2008 11:37 pm Post subject: |
|
|
Override wrote: | yea but how to include test1.php?i=1 in another script |
All you have to do is set the variable in the script and then include the file.
test.php
Code: | <?php
$i = 0;
include("test1.php");
?> |
test1.php
Code: | <?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;
?> |
calling test.php will echo "i equals 1" to the page _________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
Override -
Joined: 25 Nov 2003 Posts: 124 Location: Virtual Abyss N Area 61
|
Posted: Tue Jun 24, 2008 11:59 pm Post subject: |
|
|
that worked but the
Code: | <?php
$i = 0;
include("test1.php");
$i =1;
include("test1.php");
?> | 1 does not it just shows 0 like a loop or somthing |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Wed Jun 25, 2008 12:27 am Post subject: |
|
|
Override wrote: | that worked but the
Code: | <?php
$i = 0;
include("test1.php");
$i =1;
include("test1.php");
?> | 1 does not it just shows 0 like a loop or somthing |
Instead of including the file every time you want to call the switch statement, put the switch statement inside a function and call the function
Code: | <?php
function switch_function($i){
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;
}
switch_function(0);
switch_function(1);
?> |
You can call it something else other than switch_function, its the first thing that popped into my head! Just don't call it switch!
You can save the function in a file and include it at the top of a script when you want to use it
test.php
Code: | <?php
include("switch_function.php");
switch_function(0);
switch_function(1);
?> |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
Override -
Joined: 25 Nov 2003 Posts: 124 Location: Virtual Abyss N Area 61
|
Posted: Wed Jun 25, 2008 1:09 am Post subject: |
|
|
ok ??? something happen there nuthing worked & got a little lost ?? |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Wed Jun 25, 2008 9:19 am Post subject: |
|
|
Override wrote: | ok ??? something happen there nuthing worked & got a little lost ?? |
switch_function.php
Code: | <?php
function switch_function($i){
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;
}
?> |
test.php
Code: | <?php
include("switch_function.php");
switch_function(0);
switch_function(1);
?> |
test.php wrote: | i equals 0i equals 1 |
Does that help? _________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
Override -
Joined: 25 Nov 2003 Posts: 124 Location: Virtual Abyss N Area 61
|
Posted: Wed Jun 25, 2008 9:50 am Post subject: |
|
|
ty that worked i am havein to relearn this agan damn fast cars. |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Thu Jun 26, 2008 8:36 pm Post subject: |
|
|
Override wrote: | ty that worked i am havein to relearn this agan damn fast cars. |
Glad I could help :) _________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
|