What is the problem in this code?


#1


what is the rpoblem? it doesnt work!


<!DOCTYPE html>
<html>
    <head>
		<title></title>
	</head>
	<body>
    <?php
     $name=3;
  
     switch($name)
     case 0:
         echo '1 droste';
         break;
         case 1:
         case 2:
             echo '2 droste';
            break;
             case 3:
                 echo '3 droste';
                 break;
                 default:
                 echo ' har chi bgi to rast migi! ';
    ?>
	</body>


#2

I believe your switch statement needs some curly braces.

switch($name){
//code
}

or

switch($name):
//code
endswitch;


#3

@marzie.89

= 1 ========================================

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>
      <?php
         echo "Test output";
      ?>
    </p>
  </body>
</html>

= 2 =========================================================

switch (parameter-expression) {
  case value1:
    //Statements executed 
    //      when the VALUE of expression matches value1
    [break;]
  case value2:
     //Statements executed 
    //      when the VALUE of expression matches value2
    [break;]
 ...
  case valueN:
    //Statements executed 
    //      when the VALUE of expression matches valueN
    [break;]
  default:
    //Statements executed 
    //      when none of the VALUE's match the VALUE of the expression
    [break;]
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

The switch statement evaluates an parameter-expression,
matching the parameter-expression's VALUE to a case clause,
and executes statements associated with that case.
( and with the break or continue-statement will exit the switch statement )
[end-quote]

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

So in this situation
we use a variable $name as a parameter-expression
and this variable has a number Value
so in your case clause you will have to use a number Value
which will be compared to the parameter-expression-VARIABLE-number-VALUE

     switch($name) {
        // encapsulate the =case='s in a pair of curly-brackets-{ }
     case 0:
         //why do you give an =echo= '1 droste' for a case-clause of 0
         echo '1 droste';
         break;
      case 1:
      case 2:
         //As =case 1:= has no statement to execute, it is called a =fall-trough=
         //now for both a =case= of 1 and 2 are going to =echo= '2 droste'
         echo '2 droste';
         break;
      case 3:
         echo '3 droste';
         break;
      default:
        echo ' har chi bgi to rast migi! ';
}

Reference::
http://php.net/manual/en/language.basic-syntax.comments.php
http://php.net/manual/en/control-structures.switch.php
http://php.net/manual/en/control-structures.continue.php
http://php.net/manual/en/function.return.php
http://php.net/manual/en/control-structures.alternative-syntax.php


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.