5

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.)

isekaijin
  • 19,076
  • 18
  • 85
  • 153
  • @Neal: Because it's not a good idea to mix code and content. – isekaijin May 09 '11 at 17:38
  • @ Eduardo León: If the function is part of the display logic (say, a logic that requires recursion), then it may be a good idea. Depends on the situation. – webbiedave May 09 '11 at 17:49

5 Answers5

4
<?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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • 1
    -1; this is a mess. The (illegal) use of `//` comments inside HTML is strange and makes for confusing reading. The English throughout is barely comprehensible, especially the sentence *"= is not allowed in php for tags is allowed if Set"*, which I can't make any sense of. In your second example here you've broken the syntax of the `=` block from the question by (illegally) adding an `echo` to it; I *think* you're later attributing the syntax error to `short_open_tag` not being `On`, but that's untrue; it's a syntax error *regardless* of that setting. – Mark Amery Jan 06 '19 at 18:07
2

If short_open_tags are on, yes, it's possible.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
  • I was actually asking whether it was valid to use inline HTML inside a function, regardless of my use of `=`. And, yep, I do have `short_open_tags` on. – isekaijin May 09 '11 at 17:45
  • @Eduardo León: _Validity_ isn't really at issue here, you could even use _asp_tags_ (`<%= %>`) if you've got them enabled. ;-) – stealthyninja May 09 '11 at 17:48
2

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

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
David Fells
  • 6,678
  • 1
  • 22
  • 34
1

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

1

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:

  1. 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.

  2. 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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459