Jump to content

If OR Statement fail


guymclaren272

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
https://forums.phpfreaks.com/topic/273943-if-or-statement-fail/
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;

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

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.