PHP换行符不起作用

时间:2014-12-24 12:05:01

标签: php line-breaks

我正在学习php并且正在制作一个声明,证明局部变量不能在定义它的函数之外调用。

我似乎无法在声明中找到换行符,但只有一个行空间。

<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>

<?php 

//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!";

function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!";
print($l_test_string);
}

//Calling statements

printstring();
echo "\r";
echo 'Global variable value: ' . $g_test_string;
echo 'Local variable value: ' . $l_test_string;

?>

</body>
</html>

打印输出如下:

this is the value of the variable inside the function at a local level! Global variable value: this is the value in the variable outside the function at a global level! Local variable value:

我尝试在函数调用和echo for global变量之间中断,但只生成行空间而不是中断。

想象一下我想象的简单。

非常感谢

安德鲁

3 个答案:

答案 0 :(得分:2)

如果您想确保您的换行符与跨平台兼容,请使用PHP_EOL我通常使用“\ n”。如果您希望换行符显示在HTML中,请使用"<br>"

编辑以回应评论: 这是你如何从HTML中尽可能多地利用PHP。

<?php 

//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!";
?>
<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>
<?php
//Calling statements

printstring();
echo "<br>\n";
echo 'Global variable value: ' . $g_test_string."<br>\n";

echo 'Local variable value: ' . $l_test_string."<br>\n";
?>    
</body>
</html>

<?php
  function printstring()
  {
    $l_test_string="this is the value of the variable inside the function at a local level!";
    print($l_test_string);
  }

答案 1 :(得分:1)

您希望PHP中的换行符使用<br />\n

我为你的演示使用了breaklines:http://www.forumalliance.net/so.php

<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>

<?php 

//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!<br />";

function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!<br />";
print($l_test_string);
}

//Calling statements

printstring();
echo "\r";
echo '<br />Global variable value: ' . $g_test_string;
echo '<br />Local variable value: ' . $l_test_string;

?>
</body>
</html>

答案 2 :(得分:0)

如何在while循环中逐行断开显示数据的行并转发到下拉列表

$que="select * from parties  WHERE Item_code='".$_GET['q']."'";

$run=mysql_query($que);

while($row=mysql_fetch_array($run)){    
  if($row['party_name'] == "")
  {
    echo "";
  }

  else
  {
    if($_GET['type'] == "di")    
    {
      echo $row['Id'];      
    }    
    elseif($_GET['type'] == "did")
    {    
      echo  $row['party_name'];     
    }//END IF DI OR DID    
  }//END IF PARTY NAME BLANK    
}//END WHILE