douhutongvm382381
2012-12-06 10:22
浏览 63
已采纳

或 在php中回显不打印[重复]

Possible Duplicate:
Print newline in PHP in single quotes
Difference between single quote and double quote string in php

$unit1 = 'paragrahp1';
$unit2 = 'paragrahp2';
echo '<p>' . $unit1 . '</p>
';
echo '<p>' . $unit2 . '</p>';

This is displaying (on view source):

<p>paragraph1</p>
<p>paragraph2</p>

but isnt what I’m expecting, not printing the new line, what can be?

图片转代码服务由CSDN问答提供 功能建议

可能重复:
用单引号打印PHP中的换行符
php中单引号和双引号字符串之间的区别

  $ unit1 ='paragrahp1'; 
 $ unit2 ='paragrahp2'; 
echo'&lt; p&gt;'  。  $ unit1。  '&lt; / p&gt; 
'; 
echo'&lt; p&gt;'  。  $ unit2。  '&lt; / p&gt;'; 
   
 
 

这显示(在查看源上):

 &lt;  p&gt;段落1&lt; / p&gt; 
&lt; p&gt;段落2&lt; / p&gt; 
   
 
 

但不是我所期待的,不是打印新行,什么可以 是吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • douyun1860 2012-12-06 10:23
    已采纳

    PHP only interprets escaped characters (with the exception of the escaped backslash \\ and the escaped single quote \') when in double quotes (")

    This works (results in a newline):

    "
    "
    

    This does not result in a newline:

    '
    '
    
    已采纳该答案
    打赏 评论
  • dongwei3151 2012-12-06 10:22

    Escape sequences (and variables too) work inside double quoted and heredoc strings. So change your code to:

    echo '<p>' . $unit1 . "</p>
    ";
    

    PS: One clarification, single quotes strings do accept two escape sequences:

    • \' when you want to use single quote inside single quoted strings
    • \\ when you want to use backslash literally
    打赏 评论
  • duandeng2265 2012-12-06 10:23

    must be in double quotes!

    echo "hello
    world";
    

    Output

    hello
    world
    

    A nice way around this is to use PHP as a more of a templating language

    <p>
        Hello <span><?php echo $world ?></span>
    </p>
    

    Output

    <p>
        Hello <span>Planet Earth</span>
    </p>
    

    Notice, all newlines are kept in tact!

    打赏 评论
  • dongtun3259 2012-12-06 10:23

    must be in double quotes!

     echo '<p>' . $unit1 . "</p>
    ";
    
    打赏 评论
  • dtg25862 2012-12-06 10:25

    Better use PHP_EOL ("End Of Line") instead. It's cross-platform.

    E.g.:

    $unit1 = 'paragrahp1';
    $unit2 = 'paragrahp2';
    echo '<p>' . $unit1 . '</p>' . PHP_EOL;
    echo '<p>' . $unit2 . '</p>';
    
    打赏 评论
  • dragon5006 2012-12-06 12:15
    $unit1 = "paragrahp1";             
    $unit2 = "paragrahp2";  
    echo '<p>'.$unit1.'</p>';  
    echo '<p>'.$unit2.'</p>';
    

    Use Tag <p> always when starting with a new line so you don't need to use /n type syntax.

    打赏 评论

相关推荐 更多相似问题