Is this the correct way of putting HTML in PHP?

前端 未结 3 2026
不知归路
不知归路 2021-01-21 12:28

I\'m new to PHP, and most of the time I have been \'echo\'ing my HTML. Today, I found this way of doing it, which makes things 1000 times easier:



        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-21 12:36

    It's mostly about how you like to read your code.

    With IDE that has autocomplete/code suggestions it is best to use so that autocompletion/suggestion features know when to use html and when php suggestions for your code. And of course it's nice to select coding style that is easy to read later.
    As far as I know code highlight wont work, and should not work, inside

    echo '

    ...';

    Different coding styles:

    Other than what already mentioned there is no big differences. For example using

    echo "
    $var1

    $var2

    ...";

    for inserting variables it is easier (less writing), code has smaller footprint and easier to read than this:

    ...

    Short open tags / why not use:

    There is also shortcut available but I have not used it since it requires PHP configuration (which is not always available/changeable/writable) value short_open_tag to be set. However PHP manual says Since PHP 5.4.0,
    See Are PHP short tags acceptable to use?
    and PHP echo vs PHP short tags

    And what's found under hood

    Everything inside is parsed by PHP: Hypertext Preprocessor so someone may say that it takes much more resources to use echo ''; but this is again wrong place to do optimization if you don't have over 100000 echo's or print's + starting and ending ?> takes few instructions too as does echo so they are almost same when counting cpu instructions, at least with my installation.
    See How exactly is a PHP script executed?
    and php.net/manual/en/tokens.php
    and PHP Compiler Internals slides by Sebastian Bergmann

    Some tests about performance:

    1.77s.: 1 000 000 times $var = rand(0, 1); echo 'Foo Bar '.$var;
    1.61s.: 1 000 000 times $var = rand(0, 1); ?>Foo Bar
    1.83s.: 1 000 000 times $var = rand(0, 1); echo "Foo Bar $var";

    I really think that it is most about how you like to write/read your code. Sometimes it's better to write and later read
    if (...) { echo "something"; }
    and sometimes
    something

    Personally I prefer using echo "..."; when there is a lot of variables or only small piece of text printed out and ?>... when outputting large chuncks of html or long text with only few variables. This makes code easier to read.

    About accelerators mentioned in comments:

    If I got it right, php does lexical analysis first and passes results to accelerator, from this point of view ?>text and echo "text"; has big difference but it still does not change performance within normal website projects.
    More clear: Accelerator itself does improve performance but about using ?>text or echo "text"; does not make big difference.

    Final answer:

    Yes, it is widely accepted and used but I think (at least did not find anything clear from PHP docs) it is still undocumented way, However, as you already know it is not only way. Basically it does just same job as if using echo:

    if ( get_field('field_name') ) {
       echo "

    HTML can go here by echo'ing it

    "; }

    While trying to find official docs I have read these pages:
    php.net/manual/en/control-structures.alternative-syntax.php
    php.net/manual/en/language.basic-syntax.phpmode.php
    php.net/manual/en/language.types.string.parsing.complex
    used google, and searched some forums.

    It seems that clearly documented alternate way is not used so often:

    
    A is equal to 5
    
    

    UPDATE:

    Official documentation about separating { and } by and ?> within conditional statements is here: control-structures.intro and language.basic-syntax.phpmode.php together documented it, however there is demonstration only for alternate way to do it (see above).

提交回复
热议问题