Jump to content
elentz

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

Recommended Posts

Posted (edited)

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

Share this post


Link to post
Share on other sites

You are doing comparisons instead of assignments

Share this post


Link to post
Share on other sites

ugh I see what you are saying now.  How can I set the serverip to either of the other two variables depending on the condition?

Share this post


Link to post
Share on other sites

I figured it out, thanks for pointing that out to me.

Share this post


Link to post
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;

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.