-3
<?php switch (true) :
    case $_GET['change']:
      echo 'hello';
    break;
    default:
      echo 'bye';
    break; ?>

It gives me this error:

Parse error: syntax error, unexpected end of file in D:\xampp\htdocs\profile.php on line 47.

And that is the end of my code. What did i forgot because if I comment this piece of code it works well.

tRx
  • 815
  • 1
  • 15
  • 24
Jelle N
  • 1
  • 3

6 Answers6

3

The correct code is:

<?php 
    switch (true) :
        case $_GET['change']:
            echo 'hello';
            break;
        default:
            echo 'bye';
            break; 
    endswitch;
?>

It is because you miss endswitch; or just use brackets {}

Eimsas
  • 492
  • 6
  • 21
  • One more question, is this the right url http://localhost/profile.php?change?? – Jelle N Feb 22 '17 at 13:08
  • It is valid url, but if its correct I don't know, if the page /profile.php exists then yes it is right url, ?change is the $_GET parameter. You can use it like that ?change=yourValue – Eimsas Feb 22 '17 at 13:10
3

Your switch case need a variable to handle the case like:

<?php
switch($_GET["change"])
{
   case "value":
     echo "You called this case.";
     break; // Stop the switch for being executing futher.

   default:  // Set default message if the case wasn't found.
     echo "Cannot find the case.";
     break;

}
?>

Read more: http://php.net/manual/en/control-structures.switch.php

node_modules
  • 4,790
  • 6
  • 21
  • 37
2

You're using the alternative syntax for control-structures, which means you have to close your switch at the end, with this

endswitch

making your full code

switch (true):
    case $_GET['change']:
        echo 'hello';
        break;
    default:
        echo 'bye';
        break; 
endswitch;
?>

Although, you can simplify this piece of code to the following, unless you intend to expand your switch/case structure.

echo isset($_GET['change']) ? "hello" : "bye";

In any case, just doing case $_GET['change']: just checks if that holds any values, not what they are, or if its set at all, so you may get warnings about this. If you intend to expand your structures, you should see the example of @C0dekid - that's how you should build your switch/case

Qirel
  • 25,449
  • 7
  • 45
  • 62
1

It should be as following:

<?php 
switch ($_GET['change']) {
case [YOURVALUEHERE]:
  echo 'hello';
break;
default:
  echo 'bye';
break; 
}
?>

Example from PHP.net

<?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";
}
?>
1

It is better to set brackets:

<?php
switch ($_GET['change']) {
    case true:
        echo 'hello';
    break;
    default:
        echo 'bye';
    break;
}
?>

If you want to use the alternate syntax take a look at this warning:

Warning

Any output (including whitespace) between a switch statement and the first case will result in a syntax error. For example, this is invalid:

Community
  • 1
  • 1
Roman
  • 2,530
  • 2
  • 27
  • 50
0

You didn't put right enclosure for your switch, dont use ":" but "{". For example:

switch (true) {
case $_GET['change']:
  echo 'hello';
break;
default:
  echo 'bye';
break;
}
ermacmkx
  • 439
  • 5
  • 12