Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is inline HTML valid inside a PHP function?

Tags:

php

Is the following valid PHP?

<?php
function a($a) {
?>
    <p><?=$a?></p>
<?php
}
?>

(I know this is not a good idea, just want to know if it's possible.)

like image 848
pyon Avatar asked May 09 '11 17:05

pyon


1 Answers

<?php
function a($a) {
?>
    <p><? echo $a?></p> //echo variable content's 

// <? ?> is allowed only if you have **Enabled short_tages in php.ini file**
<?php
}
?>

Enable short_tages in php.ini file

<?
$a="stackoverflow";
function a($a) {
?>
    <p><?= echo $a?></p>
<?
}
a($a);
?>

If you try to run this program using <?= this won't allowed it will give you error

Parse error: syntax error, unexpected T_ECHO

<?= is not allowed in php for tags <? is allowed if Set

short_open_tag=On

in php.ini

As of PHP 5.4.0, <?= ?> PHP tags are available regardless of short_open_tag ini setting.

like image 156
Vishwanath Dalvi Avatar answered Oct 14 '22 20:10

Vishwanath Dalvi