Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 elentz

elentz
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 03 May 2018 - 02:32 PM

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, 03 May 2018 - 02:33 PM.


#2 benanamen

benanamen
  • Members
  • PipPipPip
  • Master Coder
  • 1,564 posts

Posted 03 May 2018 - 02:38 PM

You are doing comparisons instead of assignments

To save time, let's just assume I am never wrong.

The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.


Make A Donation https://www.paypal.me/KevinRubio

 

"This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"


#3 elentz

elentz
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 03 May 2018 - 03:09 PM

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?



#4 elentz

elentz
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 03 May 2018 - 03:19 PM

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



#5 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,722 posts
  • LocationLos Angeles, CA USA

Posted 03 May 2018 - 03:41 PM

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;


#6 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,937 posts
  • LocationCanada

Posted 03 May 2018 - 09:15 PM

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, 03 May 2018 - 09:15 PM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users