4

When writing PHP code in the past, I have often been plagued by the awkwardness of having to nest my HTML code in calls to print, echo or similar. This is alleviated to some degree by the ability to make parts of the code be literally outputted by closing the PHP tag and reopening it again after the output, eg:

<?php /*DoSomeStuff*/ ?>
Some HTML code.
<?php /*SomeMorePHP*/ ?>

However, I have never been clear on how this interracts with functions. For example, it is unclear to me if writing:

<?php
function myFunction() {
?>
Some HTML
<?php
}
?>

Will produce a function which upon being called will output that HTML, if the function will be parsed as empty but output that HTML during parsing, or neither, both or if this construct is just illegal entirely?

I am reluctant to base all my results on just trying this on some particular instance of PHP as I do not wish to beleive it works while in reality it might be undefined behaviour or think it doesnt work while I might just have an old or buggy PHP and I have never seen this construct used in any code.

Ideally I am looking for some kind of reference to documentation or specification which would clear this up.

Vality
  • 6,577
  • 3
  • 27
  • 48
  • why not just run the code and see what happens, there is a super secret but perfect documentation called php.net you might want to reference that :P – some_groceries Nov 23 '15 at 10:15
  • Maybe this will help - http://stackoverflow.com/questions/5940428/is-inline-html-valid-inside-a-php-function . – Alex Szabo Nov 23 '15 at 10:15
  • Modern best practices suggest you shouldn't embed html code in php or the other way round. – Calimero Nov 23 '15 at 10:18
  • @deeznutz As I said, I am reluctant to rely on running some code, where it may work but only due to a bug or particular version but not be actually well defined. Or might not work due to a particulqar version or bug while being fine code. And I have looked over php.net but not found anything about this yet. – Vality Nov 23 '15 at 10:21
  • @Calimero I entirely understand the thinking there. But in many cases this is not an easy option, particularly when working on projects that already have larger structural issues. I often try and make things as neat as possible without tearing up the whole code structure. – Vality Nov 23 '15 at 10:25
  • @Vality my bad, thought you were looking for guidance on how to avoid spaghetti code like that (which is quite a pain to read, you're absolutely right). – Calimero Nov 23 '15 at 10:29

2 Answers2

2

I know this is not exactly answering your questions with a lot of references, but: This is valid (PHP Docs), although it doesn't look very nice, it's a common practice in some old but BIG frameworks.

You can try this and see what happens:

function htmlOut() {
    ?>
    Some HTML output
    <?php
}


htmlOut();

By the way I found an example, the default skin of MediaWiki (I would say they know what they are doing) is using just the method you have described.

/**
 * Outputs the entire contents of the (X)HTML page
 */
public function execute() {
    /**
     * some code
     */
    // Output HTML Page
    $this->html( 'headelement' );
    ?>
    <div id="mw-page-base" class="noprint"></div>
    <div id="mw-head-base" class="noprint"></div>
    <div id="content" class="mw-body" role="main">
        <a id="top"></a>

        <?php
    /**
     * some more code
     */
}

See the full code here: MediaWiki GitHub

swidmann
  • 2,787
  • 1
  • 18
  • 32
  • Thank you for finding an example of a commonly used script that uses this construct. That gives me a lot of confidence its unlikely to be just an implementation bug. – Vality Nov 23 '15 at 10:32
1

Basically it will work as you expect it to be, which means that this HTML will be printed only on function invoke.

There's probably no documentation for your use case, but it's similar to condition expressions.

You could ask if similar question for code below:

<?php if ($expression == true): ?>
  HTML1
<?php else: ?>
  HTML2
<?php endif; ?>

Will PHP print both HTML parts, or only one depending on the condition? Well, the doc says clearly that it works as it is expected to be. I think we can say it's the same for functions/methods, because it's just "a block of code". It works with the same rule in many other cases like loops or swich

Reference: http://php.net/manual/en/language.basic-syntax.phpmode.php Example #1

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64