/    Sign up×
Community /Pin to ProfileBookmark

I have just started learning PHP, and have got so far as the switch statement, and have grinded to a halt- I don’t understand how it works. Is there anywhere else which has decent tutorials for learning the stuff I don’t understand at w3schools?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYDec 06.2005 — Hope it's not too unclear:

[code=php]
<?php
# let's take an easy example: a php-based navigation
# the page name is defined in the URL
# e.g.: http://www.example.com/index.php?page=contact
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
# set page to contact if it is not defined in the URL
$page = "contact";
}
switch($page){
case "contact";
# statements if page = contact
# e.g. include the contact.php page
include "contact.php";
# you have to break out of the switch
break;
case "something";
# statements if page = something
# ...
# remember to break
break;
#... add as many cases as needed
# the last one is just like the "else" of an "if-else" statement
# it is called the default case
case default;
# statements in the default case
# that will apply if the 'page' defined in the URL
# is none of the cases of the switch structure
# e.g. if the user enters http://example.com/index.php?page=blue
# and there is no blue case, then the default case will be applied
# meaning: contact.php will be included
include "contact.php";

# and break;
break;
}
?>
[/code]
Copy linkTweet thisAlerts:
@phpmyborderDec 06.2005 — $num = rand(1,3); // set num = a random number between 1 and 3

switch($num){ // checks the value of num (which random was picked)

CASE 1: echo "Jippy, the random number is 1 !"; // prints

break; // always end a CASE with break;

CASE 2: echo "Jippy, the random number is 2 !";

break;

CASE 3: echo "Jippy, the random number is 3 !";

break;

}



Learning by doing, try it your selv..
Copy linkTweet thisAlerts:
@NogDogDec 06.2005 — The online manual entry for switch gives a pretty readable explanation of how it works: http://www.php.net/switch
Copy linkTweet thisAlerts:
@ShrineDesignsDec 06.2005 — a switch is much like a group of if/else statements

example[code=php]$var = '3';

if($var == 1)
{
// ...
}
else if($var == 2)
{
// ...
}
else if($var == 3)
{
// ...
}
// or
switch($var)
{
case 1:
// ...
break;
case 2:
// ...
break;
case 3:
// ...
break;
}[/code]
Copy linkTweet thisAlerts:
@tims15authorDec 07.2005 — Thanks for that, I now understand!

The only thing I don't get is why do you need a break at the end of each case?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYDec 07.2005 — http://www.php.net/switch
It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. [/QUOTE]
Copy linkTweet thisAlerts:
@tims15authorDec 09.2005 — Thanks for that, I understand it now. Im slowly progressing through W3schools!
×

Success!

Help @tims15 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...