Jump to content

if/else error I get "syntax error, unexpected '}' "


elentz

Recommended Posts

I am getting the following error "syntax error, unexpected '}'"  I don't see the error, all the brackets are where they need to be aren't they?

$remote = Yes;

if ($remote == "Yes"){
$serverip == $serverwanip;
} else {
$serverip == $serverlocalip;
}
echo $serverip;

There is no echo of $serverip

 

Thanks for looking

Link to comment
Share on other sites

Just for the sake of posterity and future google results:

 

Your code should have been:

 

if ($remote == "Yes"){
    $serverip = $serverwanip;
} else {
    $serverip = $serverlocalip;
}
This is a common pattern, and many languages (PHP included) have the ternary operator to reduce this to one line.

 

$serverip = ($remote == 'Yes') ? $serverwanip : serverlocalip;
Link to comment
Share on other sites

Just for the sake of posterity and future google results:

 

Your code should have been:

 

if ($remote == "Yes"){
    $serverip = $serverwanip;
} else {
    $serverip = $serverlocalip;
}
This is a common pattern, and many languages (PHP included) have the ternary operator to reduce this to one line.

 

$serverip = ($remote == 'Yes') ? $serverwanip : serverlocalip;

 

Yes. And you can go one further and use a Boolean value (i.e. True/False) for the $remote value. Shouldn't be using Yes/No anyway when you really want a Boolean. Then it becomes even simpler since no comparison is even needed:

$remote = True; // or False
 
$serverip = ($remote) ? $serverwanip : serverlocalip;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.