Escape HTML to PHP or Use Echo? Which is better?

前端 未结 13 758
无人共我
无人共我 2020-12-01 04:34

In terms of performance , what would be better. Using PHP to echo all the HTML output so I can pepper it with the various bits of working code and variables or escape HTML t

13条回答
  •  猫巷女王i
    2020-12-01 04:53

    It's all about which you find the most readable. Of course, this will vary with each situation. If you were doing an entire page, and there were large sections which did not have any PHP in it, then I'd break out of PHP and just write the plain HTML, whereas if there was a section which had a lot of PHP variables, I'd do it all in PHP.

    For example:

    , , and

    versus:

    "
        . ""
        .    ""
        .        $a . ", " . $b . " and " . $c
        .    ""
        . ""
        . ""
    ; ?>
    

    Or

    
             
                
                   {$a}, {$b}, and {$c}
                
             
          ";
    ?>
    

    Also don't forget about printf

    "
        . ""
        .    "%s, %s and %s"
        . ""
        . ""
        , $numCols
        , $a
        , $b
        , $c
    );
    ?>
    

提交回复
热议问题