Reputation: 17
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
Upvotes: 2
Views: 4567
Reputation: 57268
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.
Upvotes: 4
Reputation:
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 }
}
?>
Upvotes: 2
Reputation: 6782
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.)
Upvotes: 1
Reputation: 3217
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 }} ?>
Upvotes: 3
Reputation: 1845
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; ?>
Upvotes: 2