What is the best way to echo results from the database into html code in PHP?

后端 未结 5 2028
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 05:04

when I have a value like this in the database (\"foo\")

how can I echo it without any conflict with html code

notice



        
5条回答
  •  猫巷女王i
    2021-01-15 05:40

    First, don't use short tags ('

    Next, your HTML is malformed because you've got an extra set of quotes. Since you seem to be taking the approach of embedding PHP into the HTML, then a quick fix is:

    
    

    ...although since this value is coming from your database it will be stored in a variable, probably an array, so your code should look more like:

    
    

    For clarity, most programmers would try to eliminate switching between PHP parsed and non-parsed code either using a template system like smarty or....

    \n";
     ....
    ?>
    

    (Note that

    1) when the variable is within double quotes with a block of PHP, the value is automatically substituted

    2) when refering to an associative array entry within a double quoted string, the index is NOT quoted.

    HTH

    C.

提交回复
热议问题