2014-10-11 89 views
0

我已經開始學習PHP了。在我的第一個代碼中,換行符不正確。我已經閱讀了PHP文檔,但仍然沒有解決問題。換行時不會出現換行符

<?php 
    # Echoing 
    echo "Hello World to PHP \n"; 
    echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php"; 

    # Variable Basics 
    $name = "Vivek Kumar"; 
    $age = 26; 
    echo "My name is $name and age is $age ."; 
    echo 'My name is '. $name . ' and age is ' . $age . '.'; 
?> 

輸出:

的Hello World到PHP並置在PHP中使用.Vivek kumarLearning phpMy名行是維韋克·庫馬爾和年齡是26。我的名字是 維韋克·庫馬爾和年齡是26。

+1

如果您在瀏覽器中看到結果,那麼用戶
'標籤 – Girish 2014-10-11 09:35:39

+0

如果您在瀏覽器中查看該標籤,請使用
標籤,否則如果您在客戶端查看該標籤,請使用「\ n」。 – SuperBear 2014-10-11 09:47:17

+0

看看我的答案,知道如何使用「/ n」作爲瀏覽器中的換行符,或者只是簡單地使用
標記換行謝謝 – 2014-10-11 09:50:33

回答

1

您可以使用nl2br轉換新的生產線(\n)到換行符(<br>)。

http://php.net/manual/en/function.nl2br.php

string nl2br (string $string [, bool $is_xhtml = true ]) 

返回字符串<br /><br>所有換行符(\r\n, \n\r, \n and \r)之前插入。

或者您可以像其他人所建議的那樣使用換行符。從表單文本區域顯示文本時,nl2br非常方便。

+0

謝謝我不知道這個api。 – dearvivekkumar 2014-10-13 06:06:45

2

我假設你將輸出回顯到你用網絡瀏覽器看的html頁面中?在這種情況下,將換行複製到輸出,但是它們不是可視化的。這只是因爲在HTML標記中,換行符與純文本文件不同。


檢查有關使用<br><br />換行符這樣的標記。此外,PHP提供了這種功能的方便nl2br()功能。

此類輸出的一個例子(見它here):

Hello World to PHP Concatenation in PHP is done using . 
<br /> 
Vivek kumarLearning phpMy name is Vivek Kumar and age is 26 . 
<br /> 
My name is Vivek Kumar and age is 26. 

但總的來說,你應該考慮一下是否你真的只想把它們中的HTML標記時來連接這些字符串。通常情況下,您希望將它們包裝在不可見的容器中,如跨度,div或段落,以便您可以使用樣式(樣式表/ css)控制最終佈局。

一個任意的例子(見它here

HTML:

<div id="intro">Hello World to PHP Concatenation in PHP is done using .</div> 
<h2>Vivek kumarLearning php</h2> 
<div class="plain">My name is Vivek Kumar and age is 26 .</div> 
<div class="plain">My name is Vivek Kumar and age is 26.</div> 

CSS:

body { 
    font-size: 130%; 
} 
#intro { 
    font-weight: bold; 
    margin-bottom: 10px; 
} 
.plain { 
    font-size: 100%; 
} 
0

,如果你在瀏覽器中使用這個你應該使用<br>標籤或你應該使用<pre> 我發佈的答案與<pre><br>

**with `<pre>`** 

    <?php 
     # Echoing 
     echo '<pre>'; 
     echo "Hello World to PHP "."\n"; 
     echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php"; 
     echo '<pre>'; 


   echo "Hello World to PHP "."<br/>"; 
      echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";