-1

Inside the body of an html document I have something like this:

<?=str_replace(' ', '_', $result[0]['something'])?>

This works perfectly fine. In the same document I have this:

<?php if(!empty($result[0]['something'])) { echo "Hello"; } else { echo "&nbsp;"; }?>

Which also works fine, but it slightly bothers me that I am using <?= in one place and <?php in another. When I try to change the if code to become:

<?=if(!empty($result[0]['something'])) { echo "Hello"; } else { echo "&nbsp;"; }?>

or

<?= if(!empty($result[0]['something'])) { echo "Hello"; } else { echo "&nbsp;"; }?>

Both result in a Parse error: syntax error, unexpected 'if' (T_IF) in....

I've attempted to find some documentation on the respective differences between <?php and <?= as a php opening tag but all I get is data on short tags - which this is not. Can someone explain this behavior for me?

1 Answers1

1

<?= is like <?php echo. You can't echo an if statement.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335