Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Short hand tags 7.x

Just wondering, I have a codeigniter project that I have been asked to do some work on. Im also trying to (where I can) prepare the project for upgrading to php 7.x (currently on 5).

One thing Im confused about is the use of php short tags. I love them but it seems they are being removed in 7?

So my question is, should I be removing them? The problem is, they make the code so much more readable, why are they removing it?

So for example:

<?php if ($product->price_discounted > 0)
{
    echo "<p class='discounted price'>" . $product->price_discounted . "</p>" 
}
?>

vs

<? if ($product->price_discounted > 0): ?>
    <p class='discounted price'><?= $product->price_discounted ?></p>
<? endif; ?>

I would much rather the second version, but whenever I run my project on php7 these don't work. Am I missing something or are they really removed?

FYI: I have short tags on in php.ini

like image 359
Brad Avatar asked Jun 16 '17 07:06

Brad


3 Answers

Important Update:

Short tags are not deprecated anymore. Despite the RFC passing, some controversy ensued and the PHP internals group refused to implement it1. Check out the migration guide for confirmation:

https://www.php.net/manual/en/migration74.php


Original Post:

Short open tags are deprecated in PHP 7.4, and will be removed in PHP 8.

https://wiki.php.net/rfc/deprecate_php_short_tags

Also, the short echo (<?=) is not part of short_open_tag as of 5.4. It is always available, and is not part of the deprecation.

https://wiki.php.net/rfc/shortags

like image 137
Tin Can Avatar answered Sep 28 '22 20:09

Tin Can


Shorthand tags are still in PHP7, the tags being removed are:

  • <% opening tag
  • <%= opening tag with echo
  • %> closing tag
  • (<script\s+language\s*=\s*(php|"php"|'php')\s*>)i opening tag
  • (</script>)i closing tag

https://wiki.php.net/rfc/remove_alternative_php_tags

like image 45
Phil Young Avatar answered Sep 28 '22 19:09

Phil Young


Is the correct php.ini being loaded?

<?php phpinfo(); ?>

Check for Loaded Configuration File

like image 43
loadiam Avatar answered Sep 28 '22 19:09

loadiam