Search code examples
phphtmlechophp-shorttags

Are the short echo tags intended to use only when PHP code is embedded into HTML code?


I tried to use short echo tags i.e. <?= ?> to display a string into my PHP file which already has PHP code present within <?php ?> tags. So, I tried to use the short echo tag within my code and got parse error.

See below code :

<?php

 echo 'if you want to serve PHP code in XHTML or XML documents,
       use these tags'; 

 <?= 'While this is going to be parsed.'; ?>

?>

I got following output :

Parse error: syntax error, unexpected '<', expecting end of file in

So, my question is: Are the short echo tags intended to be used only when PHP code is going to embed into HTML code and that's too not within already existing php pair of tags?

Can's I use in a file which contains pure PHP code only?


Solution

  • Yes you can use Short tag in a file which contains pure PHP code only. Here you are using php opening short tag inside php tags, that's why its throwing Parse Error.

    if yo want to use, then you should try like below .

    <?php
    
     echo 'if you want to serve PHP code in XHTML or XML documents,
           use these tags'; 
    ?>
    <?= 'While this is going to be parsed.'; ?>