PHP unexpected elseif

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

Answers


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 }
}
?>

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.


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 }}  ?>

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


Need Your Help

How to define a Shared DataSource in Spring Cloud Config

java spring dependency-injection spring-boot spring-cloud

Would it be possible to set up a DataSource using Spring Cloud in which open JDBC connections could be injected into all of my Spring Boot applications?

refresh materialized view every 26th of the month in oracle

sql oracle

I have a materialized view temp_mv based on table temp.I want to refresh this materialized view every month on 26th.Please suggest.