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.)
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.)
<?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.
If short_open_tags are on, yes, it's possible.
Yes, it's possible. Seems you could have easily tested this though :) Also try to avoid using <?
, use <?php
- it always works and doesn't rely on short_tags enabled in php.ini
I think it is not a frequently used format, and doesnt follow a functional code writing paradigm. In addition, adding dynamic strings into a long inline html would eventually include more embedded echo calls, which is slower than, for eample, saving a string, and just adding the dynamic content followed by one echo/print
Based upon my experience and experimentation: yes, this works fine. Breaking out of PHP tags and writing some content is equivalent to using an echo
statement. That is, a function like this...
<?php
function hello($name) {
?><p>Hello, <?= $name ?>! How are you today?</p><?php
}
has identical behaviour to a function like this:
<?php
function hello($name) {
echo '<p>Hello, ';
echo $name;
echo '! How are you today?</p>';
}
Unfortunately, I can't find anything in the manual to back this up. The closest it gets is in Example #1 Advanced escaping using conditions on the Escaping from HTML page, which shows that it's possible to mix PHP if
statements with embedded HTML:
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
It's easy enough to test and observe that in practice, all control structures seem to work fine with embedded HTML, including for
loops and functions. However, the manual doesn't state this.
Finally, a couple of asides about formatting PHP variables into HTML, like the asker does with their $a
variable in the question and I do with my $name
variable above:
Note that since PHP 5.4, it's absolutely fine to use the <?=
short echo tag within your embedded HTML. Per the manual page linked above:
There is also the short echo tag
<?= ?>
, which is always available in PHP 5.4.0 and later.
The other answers here claiming or implying that <?=
is only usable if short_open_tags
is on are wrong, and were already wrong at the time that they were posted.
In real code, you should generally call htmlspecialchars
on text that you're templating into your HTML, like this:
<p>Hello, <?= htmlspecialchars($name) ?>! How are you today?</p>
If you don't do this, and the variable you're using can contain characters like <
and >
, you risk introducing syntax errors or unwanted tags into your HTML, and potentially open yourself up to XSS attacks.