wilfred_phang Posted January 27, 2009 Share Posted January 27, 2009 Hi, i tested for the switch statement example on the W3S php site. But i encountered error. <html><body><?phpswitch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?></body></html>The error message is as below:Notice: Undefined variable: x in C:\wamp\www\W3C\w3c.php on line 7Notice: Undefined variable: x in C:\wamp\www\W3C\w3c.php on line 10Notice: Undefined variable: x in C:\wamp\www\W3C\w3c.php on line 13No number between 1 and 3 Anybody can tell me how to solve this?Thanks! Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted January 27, 2009 Share Posted January 27, 2009 What is $x ?You need to define $x in your script. Link to comment Share on other sites More sharing options...
wilfred_phang Posted January 27, 2009 Author Share Posted January 27, 2009 I'm not so clear how to solve this. I copied this example from the W3Schools page. Can you show me how?Thanks! Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted January 27, 2009 Share Posted January 27, 2009 The first part of the switch switch ($x) has a variable called $x . This is what is compared against each case value, so if you said that $x was equal to the number 2 then case 2 would run. The problem is that you script need to set $x to be something.You need the code below if you wanted to set $x to be 2 <?php$x = 2;switch ($x){case 1:echo "Number 1";break;case 2:echo "Number 2";break;case 3:echo "Number 3";break;default:echo "No number between 1 and 3";}?> You can set $x to be anything and the case can be anything you want to match against <?php$x = "Volvo";switch ($x){case Audi:echo "Audi";break;case Volvo:echo "Volvo";break;case Merc:echo "Mercedes";break;default:echo "None of these cars";}?> Link to comment Share on other sites More sharing options...
wilfred_phang Posted January 27, 2009 Author Share Posted January 27, 2009 Thanks for your helping.Why your second example's Audi and Volvo are undefined? Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted January 27, 2009 Share Posted January 27, 2009 The case values Audi and Volvo would be triggered if $x was either Audi or Volvo. They do not need to be defined as a $ in their own right as the $x value is the $ that is used in the script. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 27, 2009 Share Posted January 27, 2009 Why your second example's Audi and Volvo are undefined?Those should have been quoted, without quotes PHP thinks they are defined constants instead of values.<?php$x = "Volvo";switch ($x){case 'Audi':echo "Audi";break;case 'Volvo':echo "Volvo";break;case 'Merc':echo "Mercedes";break;default:echo "None of these cars";}?> Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted January 27, 2009 Share Posted January 27, 2009 Whoops, my bad. Why is it the thing staring you in the face is always the thing you don't see Link to comment Share on other sites More sharing options...
wilfred_phang Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks for your both help. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.