多行文字指令多種處理方式

多行文字的echo陳述式:
$myname= "WesWang";
echo "this line is headline<br/>\n
        this line is first line.
        this line is second line.
        written by $myname<br/>\n";

多行字串賦值:
$myname= "WesWang";
$long_str= "this line is headline<br/>\n
        this line is first line.
        this line is second line.
        written by $myname<br/>\n";
echo $long_str;

當然若需要換行,還是要在換行位置放入<br/>\n,這樣編譯器才會進行換行動作。

另一種多行的echo陳述式是使用運算子<<<提供多行字串,echo緊接<<<_END,結束字串時用_END來結束,切記結束字串_END必須是新的一行且不可有任何空格或其他符號在同一行前後【除了;用以結束陳述】
多行的echo陳述式:
$myname= "WesWang";
echo<<<_END
        this line is headline<br/>\n
        this line is first line.
        this line is second line.
        written by $myname<br/>\n.
_END;

<<<_END其實就像是雙引號,兩個_END標籤之間為表述內容,也會進行判斷或運算變數、運算子等等。
使用這個標籤不必運用\n進行換行,且運用單雙引號來區隔文字作為敘述也不需要使用\來轉義。<這裡指的是網頁原始碼,非網頁本身>

另外使用_END標籤,對於開發人員而言,代表可以在PHP程式直接編寫整段HTML,並且只要以PHP變數替換特定動態部分。

多行變數賦值:
$myname= "WesWang";
$long_str2=<<<_END
this line is headline<br/>
this line is first line.
this line is second line.
written by $myname.<br/>
_END;
echo $long_str2;
其使用方法規則就大同小異了。

留言