Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use ?> or php?>

Tags:

php

the tech guy at one of my clients raised to me I wasn't using <?php everywhere and would need to change that because of security reasons. Fair point, will do my good sir! However, he also asked if I could change ?> to php?> as the closing tags and that seemed a bit odd to me. I have never seen that, nor any mention of it anywhere (Google gave me a total of 0 relevant results). And even my code editor implies that that is faulty code.

Can anyone enlighten me if it even exists and if so what benefits it offers above ?>

Thanks in advance.

like image 937
Lucas van Dongen Avatar asked Aug 09 '26 14:08

Lucas van Dongen


2 Answers

Given:

<?php
error_reporting(E_ALL);
echo "echo   ";
php?>

The output is:

echo
Notice: Use of undefined constant php - assumed 'php' in /tmp/test.php on line 4

In php?>, the php part is not a component of the "end of PHP code" marker. It is just a constant that you haven't defined just like in junk?>.

like image 62
Quentin Avatar answered Aug 12 '26 04:08

Quentin


The PHP tags docs clearly says that <?php is an opening tag and ?> is closing tag.

You can use short opening tag <? but you need to enable short_open_tag in your php.ini file first.

However in some cases it's good to omit the closing tag.

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

like image 40
Michał Szepielak Avatar answered Aug 12 '26 05:08

Michał Szepielak