0

I'm trying to write an IF statement to show a button on certain pages but it doesn't work and I'm not sure why -

            <?php if($services['name'] == 'ISBN' || 'Fulfilment' || 'Author Arena' || 'Legal Deposit and Library Registration') { ?>
            <a href="<?php echo $services['why-us']['button']['link']; ?>" class="btn btn--reverse"<?php echo $services['why-us']['button']['type'] == 'download' ? ' target="_blank" download' : ''; ?>>
                <?php echo $services['why-us']['button']['name']; ?>
            </a>
            <?php } ?>
  • 1
    ```$services['name'] == 'ISBN' || $services['name'] == 'Fulfilment' || $services['name'] 'Author Arena' ||``` ... – IsThisJavascript Nov 13 '19 at 09:09
  • _“and I'm not sure why”_ - because of [Operator Precedence](https://www.php.net/manual/en/language.operators.precedence.php) ... – 04FS Nov 13 '19 at 09:09

1 Answers1

2

You need to write $services['name'] for each comparison

 <?php if($services['name'] == 'ISBN' || $services['name']=='Fulfilment' || $services['name']=='Author Arena' || $services['name']=='Legal Deposit and Library Registration') { ?>

  <a href="<?php echo $services['why-us']['button']['link']; ?>" class="btn btn--reverse"<?php echo $services['why-us']['button']['type'] == 'download' ? ' target="_blank" download' : ''; ?>>
      <?php echo $services['why-us']['button']['name']; ?>
  </a>
<?php } ?>

OR you can use in_array()

<?php if(in_array($services['name'],['ISBN','Fulfilment','Author Arena','Legal Deposit and Library Registration'])) { ?>
  <a href="<?php echo $services['why-us']['button']['link']; ?>" class="btn btn--reverse"<?php echo $services['why-us']['button']['type'] == 'download' ? ' target="_blank" download' : ''; ?>>
      <?php echo $services['why-us']['button']['name']; ?>
  </a>
<?php } ?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53