【问题标题】:echo font color outputecho 字体颜色输出
【发布时间】:2012-06-17 03:35:43
【问题描述】:

我编写了一个以红色显示错误的 php 代码。但不知何故,这似乎行不通。这是我的代码:

<?php
...    
if(!$name || !$email || !$contact || !$itemid){
                        //if not display an error message
                        echo "<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                        }else{...


?>

【问题讨论】:

  • 如果您查看正在输出的 HTML 页面的源代码,您可能会发现问题。这是一个故障排除提示。
  • 这应该给出一个错误开始。修复它。
  • 你说的对,不用看源码,打开报错就在页面上。看起来像这样:Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' 通过告诉您问题来帮助解决问题。
  • 谢谢。从现在开始会牢记这一点。

标签: php colors echo


【解决方案1】:

问题在于您的 PHP 表达式中的引号未转义。

echo "<span style="color: red;" />...
                //^ Right here        

因为,您的 PHP echo 语句也以相同的引号开头,即 "

以下是您可以解决此问题的不同方法:

  1. 使用混合引号

    echo "<span style='color: red;' />...
          // Single quote in the HTML part
    
  2. 转义引号

    echo "<span style=\"color: red;\" />...
         // Escape the quotes so that it gets treated as string rather than a modifier
    

【讨论】:

  • 谢谢。我现在会记住这一点的。
【解决方案2】:

您正在生成无效的 HTML。 &lt;span&gt; 元素不能包含 &lt;center&gt; 元素。浏览器可能会通过移动 &lt;center&gt; 元素来纠正错误,使其位于 &lt;span&gt; 之外(因此应用于 span 的样式规则不会继承到 &lt;center&gt; 元素中)。

生成有效的标记(并将您的样式规则移动到样式表中)。

echo "<p class='error'>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</p>";

【讨论】:

  • 从技术上讲,不,但我确信这不是导致他的问题的原因。
  • 即使在 html 4.0 中,
    标签也已被弃用;考虑在 CSS 中使用“text-align:center”。
【解决方案3】:

用颜色属性中的单引号修改并删除“中心”, echo "&lt;span style='color: red;' /&gt;

【讨论】:

    【解决方案4】:
        <?php
    ...    
    if(!$name || !$email || !$contact || !$itemid){
                            //if not display an error message
                            echo "<span style='color: red;' /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>";
                            }else{...
    
    
    ?>
    

    试试这个 你正在关闭报价

    【讨论】:

    • 您能否解释一下您的答案,以便 OP 知道他的代码在哪里以及哪里出了问题。
    【解决方案5】:

    做这样的事情--

    echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';
    

    您的 "" 引号有冲突

    【讨论】:

      【解决方案6】:

      您的报价相互矛盾。要么转义它们,要么使用单引号和双引号

       echo '<span style="color: red;" /><center>Fields marked with <strong>&#40; &#42; &#421</strong> are mandatory!</center></span>';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-15
        • 2018-06-05
        • 2014-10-31
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多