switch parse error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

switch parse error

Post by michaelk46 »

I keep getting a parse error on line 4 when the code below runs

Code: Select all

 
switch ($_SERVER['REQUEST_METHOD'])
     {
    case !="POST":
        include "search.html.php";
    break;
        default:
        break;
     }
 
I can get it to run when I use an if statement, but not a switch...

Anyone know why?
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

Re: switch parse error

Post by ShadowIce »

Code: Select all

switch ($_SERVER['REQUEST_METHOD'])
     {
    case "POST":
        include "search.html.php";
    break;
        default:
        break;
     }
possibly...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: switch parse error

Post by AbraCadaver »

That's because it's not valid syntax in a case statement. You would have to do something like this (clunky and strange):

Code: Select all

switch ($_SERVER['REQUEST_METHOD'] != 'POST')
{
    case true:
        include "search.html.php";
        break;
    default:
        break;
}
 
// or
 
switch (true)
{
    case ($_SERVER['REQUEST_METHOD'] != 'POST'):
        include "search.html.php";
        break;
    default:
        break;
}
This is probably better:

Code: Select all

switch ($_SERVER['REQUEST_METHOD'])
{
    case 'POST':
        // whatever
        break;
 
    default:
        include "search.html.php";
        break;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: switch parse error

Post by michaelk46 »

Thanks Abracadaver... That fixed it...
Post Reply