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

Edited by elentz
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;
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.