【问题标题】:php print statements in separate linesphp 在单独的行中打印语句
【发布时间】:2016-09-29 13:55:26
【问题描述】:

下面的 php 脚本打印了几个语句和变量!他们打印在同一行!我想将它们一个接一个地打印在单独的行中!我该怎么做? 例如,第一个 2 语句应打印为:

用 PHP 编程很有趣

我学会了如何在 PHP 中发表评论

但目前它打印为 PHP 编程很有趣,我学会了如何用 PHP 进行评论

?php 

echo "Programming in PHP is fun";

//This is the 1st programming 4 tutorial

/*This tutorial helped me
to gain a good knowledge in php! */

print "I learnt how to comment in PHP";


echo "A variable name must start with a letter or an underscore '_' -- not a number";

echo "A variable name can only contain alpha-numeric characters, underscores (a-z, A-Z, 0-9, and _ )";

echo "A variable name should not contain spaces. If a variable name is more than one word, it should beseparated with an underscore  or with capitalization ";

$name= "My name is XXX";

$number=25;

$float_number=10.245;

$negative_number=-12;

echo($name);

echo($number);

echo($float_number);

echo($negative_number);


$age=24;

$feature =$age;



echo($feature);

$x=45;

$y=12;

echo($x+$y);


$myName="My name is 'Lasith'";

?>

【问题讨论】:

  • 使用htmlbr标签
  • 使用PHP_EOLexample:- echo($x+$y).PHP_EOL;
  • 对于 html,它是 <br>。对于文本,它是 \n\r\n,它只能在双引号中使用,或者是 PHP_EOL,正如 Anant 建议的那样,它不能在引号内使用。

标签: php printing lines separator


【解决方案1】:

如果您将它们作为纯文本回显,请在每个回显后添加一个换行符:

echo "\n";

如果您将它们回显到 HTML(在网络浏览器中),请在每个回显后添加 HTML <br> 标签:

echo "<br>";

你也可以使用字符串连接

echo "Programming in PHP is fun" . "\n";
echo "Programming in PHP is fun" . "<br>";

或在字符串中添加换行符或&lt;br&gt;标签

echo "Programming in PHP is fun\n";
echo "Programming in PHP is fun<br>";

【讨论】:

    猜你喜欢
    • 2023-01-03
    • 2016-01-31
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2018-02-09
    • 1970-01-01
    相关资源
    最近更新 更多