how to include a switch statement

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Jun 24, 2008 9:32 pm    Post subject: how to include a switch statement Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Tue Jun 24, 2008 9:56 pm    Post subject: Reply with quote

Take a look at http://old.abyssunderground.co.uk/scripts-phponefile.php
_________________
Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk
Back to top View user's profile Send private message Visit poster's website
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Jun 24, 2008 10:10 pm    Post subject: Reply with quote

yea but how to include test1.php?i=1 in another script
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Tue Jun 24, 2008 10:14 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Tue Jun 24, 2008 11:37 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Tue Jun 24, 2008 11:59 pm    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Wed Jun 25, 2008 12:27 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Wed Jun 25, 2008 1:09 am    Post subject: Reply with quote

ok ??? something happen there nuthing worked & got a little lost ??
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Wed Jun 25, 2008 9:19 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
Override
-


Joined: 25 Nov 2003
Posts: 124
Location: Virtual Abyss N Area 61

PostPosted: Wed Jun 25, 2008 9:50 am    Post subject: Reply with quote

ty that worked i am havein to relearn this agan damn fast cars.
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Thu Jun 26, 2008 8:36 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group