Jump to content

If OR Statement fail


guymclaren272
 Share

Recommended Posts

If ($Hoff == 'Nelspruit') || ($Hoff == 'Hoedspruit') || ($Hoff == 'Klaserie') || ($Hoff == 'Bushbuck Ridge') || ($Hoff == 'Hazyview') || ($Hoff == 'Graskop') OR ($Hoff == 'Sabie') || ($Hoff == 'Lydenburg') || ($Hoff == 'Pilgrims Rest') || ($Hoff == 'Barberton') || ($Hoff == 'Waterval Boven') || ($Hoff == 'Komatipoort') || ($Hoff == 'Malelane') { $addon = $addon +2; } else { $addon = $addon -100; }

 

PHP Parse error: syntax error, unexpected '||' (T_BOOLEAN_OR) in /home/lowveldn/public_html/control/publish.php on line 84

 

Can someone tell me what is wrong here please.

Link to comment
Share on other sites

All of your conditions must be contained within a single set of parens (but you may have sub-parens inside)

 

E.g.

if ($Hoff == 'Nelspruit' || $Hoff == 'Hoedspruit' || $Hoff == 'Klaserie' ||
   $Hoff == 'Bushbuck Ridge' || $Hoff == 'Hazyview' || $Hoff == 'Graskop' ||
   $Hoff == 'Sabie' || $Hoff == 'Lydenburg' || $Hoff == 'Pilgrims Rest' ||
   $Hoff == 'Barberton' || $Hoff == 'Waterval Boven' ||
   $Hoff == 'Komatipoort' || $Hoff == 'Malelane')
{
   $addon = $addon +2;
}
else
{
   $addon = $addon -100;
}

 

 

Or

if ( ($Hoff == 'Nelspruit') || ($Hoff == 'Hoedspruit') || ($Hoff == 'Klaserie') ||
 ($Hoff == 'Bushbuck Ridge') || ($Hoff == 'Hazyview') || ($Hoff == 'Graskop') ||
 ($Hoff == 'Sabie') || ($Hoff == 'Lydenburg') || ($Hoff == 'Pilgrims Rest') ||
 ($Hoff == 'Barberton') || ($Hoff == 'Waterval Boven') || ($Hoff == 'Komatipoort') ||
 ($Hoff == 'Malelane') )
{
   $addon = $addon +2;
}
else
{
   $addon = $addon -100;
}

 

 

But, a much better way to handle this is by using an array. You can replace all of the above code with just this

$hoffList = array(
   'Nelspruit', 'Hoedspruit', 'Klaserie', 'Bushbuck Ridge',
   'Hazyview', 'Graskop', 'Sabie', 'Lydenburg', 'Pilgrims Rest',
   'Barberton', 'Waterval Boven', 'Komatipoort', 'Malelane'
   );

$addon += (in_array($Hoff, $hoffList)) ? 2 : -100;

Edited by Psycho
Link to comment
Share on other sites

Your entire IF statement needs to be within Parentheses (brackets). So, something like:

 

if ($Hoff == 'Nelspruit' || $Hoff == 'Hoedspruit' || $Hoff == 'Klaserie' || $Hoff == 'Whatever else') {
$addon = $addon+2;
}
else {
$addon = $addon -100;
}

 

Should do the trick...

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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