dozr13344 2017-08-20 11:32
浏览 156
已采纳

为什么echo的输出需要在输入字段中单独放入占位符字符串的引号?

I am generating a string for placeholder dynamically via PHP echo function & if I don't put quotes around PHP tags the output only takes the first word of the string. Why so?

  1. placeholder=<?php echo "Hello World"?>
    It outputs only Hello in input field
  2. placeholder="<?php echo "Hello World"?>"(Note quotes around PHP tags)
    It outputs Hello World in input field.

Same also happens for value attribute of the input field.

  • 写回答

3条回答 默认 最新

  • donglao9606 2017-08-20 11:39
    关注

    This is due to how the placeholder attribute is constructed.

    You should conform to the standard of putting your value in between quotation marks (see this example as proof).

    It requires the values to be put in between quotations if you wish to add spaces to your value.

    So imagine when you do this:

    <input type="text" placeholder=<?php echo "Hello World"?> >
    

    You are returning this in the HTML file:

    <input type="text" placeholder=Hello World >
    

    It reads 'Hello' as it's value and then HTML sees 'World' as another attribute. This is why you need to put your value in between quotes to signify that the entire string is apart of the placeholder attribute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?