tags:

views:

109

answers:

6

I'm filtering some content on my website via country specific code, I'm trying to add else statements so it doesn't have to run each piece as individual code except whatever I try gives an error:

<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>

<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }  ?>

<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }}  ?>

The above gives the following error: Parse error: syntax error, unexpected T_ELSEIF in and refers to the first elseif

+2  A: 

instead of that format use

<?php if($foo == 1): ?>
   html code
<?php else if($foo == 2): ?>
   other html
<?php else: ?>
   more html code
<?php end; ?>
FrEaKmAn
why this format?
Sarfraz
@sAc: This style is preferred by many for this type of coding (which I still think smells of spaghetti) because it eliminates the need for curly braces. Since curly braces are what caused OP to get tripped-up, it's a fair recommendation.
grossvogel
This is what you get when you combine php logic with html, this is no different to the method he is using above.
RobertPitt
True but at least there is no headache with braces which are annoying in this case. This is good for beginner. However for a good project it would be better to implement some template system like Smarty.
cthulhu
+4  A: 

Else cannot evaluate a condition, it will be executed if all other conditions are false, think of it in terms of the default statment of a switch.

This:

<?php else(isCountryInFilter(array("nz"))) { ?>
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
    <?php }}  ?>

Needs to be this:

<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }}  ?>
DRL
Very true, but OPs description makes it sound like the parser didn't even make it this far...
grossvogel
+2  A: 

Or even better to use switch-case for many conditions. For example:

switch($x) {
  case 1:
  ?> some html <?php
  break;
  case 2: 
  ?> more html <?php
  break;
}

Instead of closing the tags you can echo/print the html.

This will work:

<?php if (function_exists('isCountryInFilter')) {
if(isCountryInFilter(array("us", "ca"))) {
?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }
}
?>
cthulhu
Appreciate all the answers I found this suggestion along with DamienL's answer fixed my error.
illtemper
A: 

u are not using elseif in last line of php.

Hardeep
"u"? Has Stackoverflow introduced an SMS interface? Anyway, this can't be it. The error is an **unexpected** elseif, not a *missing* one.
David Dorward
A: 

FrEaKmAn's answer will probably work, but I prefer the curly braces myself. Try changing

<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>

to

<?php } elseif(isCountryInFilter(array("au"))) { ?>

Also, in general, it's good to minimize the number of times you break in and out of php blocks...

(Once this piece is fixed, you'll need to deal with the problem pointed out by DamienL.)

grossvogel
What's wrong with this answer?
grossvogel
+2  A: 

As you're combining PHP with direct HTML / OUTPUT. During your code you're printing whitespace in between the ending bracket and the elseif keyword.

PHP's Interpreter looks directly after the } for the elseif keyword but what it finds is a block of outputted data, so it raises an error.

<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }  ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }}  ?>

What you need to do is to remove the whitespace like so.

<?php if (function_exists('isCountryInFilter')) { ?>
  <?php if(isCountryInFilter(array("us", "ca"))) { ?>
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
  <?php }elseif(isCountryInFilter(array("au"))) { ?>
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
  <?php }else(isCountryInFilter(array("nz"))) { ?>
    <a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>

You will notice the lack of space OUTSIDE the PHP blocks.

This should resolve your problem.

RobertPitt
+1, as this is the problem! whitespace output!
alexanderpas
Not only. else(isCountryInFilter(array("nz"))) is Wrong. Else will run if all the statements are false and it can not contain any condition. It will throw you an error.And what about the ending: <?php}}?> it will give you an error here too because there is no space between the <?php and }.
cthulhu
thats true, but i focused my comment on the error at hand, Once he fixes the error with my help above he will deal with the 2nd issue himself. but yea you are right in regards he needs to wrap the execution of the else in brackets.
RobertPitt
i just copied his code block and fixed the whitespace problem, the other problem with the else would throw a php E_PARSE error so he would of tracked that himself :/
RobertPitt