Domanda

Now that <?= is always available in PHP 5.4+, and the old rumours of it being depreciated in PHP6 are disappearing, it seems it's completely safe to use this handy little shortcut.

My question is: What's the correct syntax?

I couldn't find any examples of it on the PHP website, and it's apparently impossible to search for <?= on Google.

My understanding is that it's a shortcut for <?php echo, but doesn't that mean you should write:

<?= $string; ?>

Rather than:

<?= $string ?>?

Or does it not make any difference because there's no more instructions after the initial echo?

Thanks.

È stato utile?

Soluzione 2

According to the PHP docs, you should use:

I have <?=$foo?> foo.

Source: http://us3.php.net/echo

Altri suggerimenti

Since <?= has always been a shorthand (prior to PHP 5.4 enabled/disabled using short_open_tag), it does not change language semantic. Therefore you are free to use either <?= "hello" ?> or <?= "hello"; ?>, just like with any other PHP one-liner.

It is a matter of style more than language construct, but whatever you choose, use it consistently throughout the project to ease further maintenance and readability.

When you use the last ; is optional, the same is true for the last ; of your php file

After performing a bunch of simple tests, it appears that it doesn't really matter:

<?php echo 'hello' . "\n" ?>
<?php echo 'hello' . "\n"; ?>
<?= 'hello' . "\n" ?>
<?= 'hello' . "\n"; ?>

These all have the same output: http://codepad.org/zWUUN9Sw

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top