PHP - ข้อมูลชนิดอักขระ (String)

Written by admin    Sunday, 20 June 2010 20:58

ตัวอย่างโค้ดการต่อสตริง หรือ ต่อข้อความ ด้วยภาษา php ในรูปแบบต่าง ๆ 


Code:


<?

// แสดงข้อแตกต่างระหว่าง single quoted และ double quoted

// เครื่องหมาย "\" ใช้สำหรับการแสดงสัญลักษณ์พิเศษ



echo 'ทดสอบการแสดงสัญลักษณ์พิเศษ<br>';  
//ผลลัพธ์คือ  แสดงข้อความ "ทดสอบการแสดงสัญลักษณ์พิเศษ" พร้อมทั้งขึ้นบรรทัดใหม่


echo "ทดสอบการแสดงสัญลักษณ์พิเศษ<br><br>";   // ผลลัพธ์คือ  แสดงผลข้อความ พร้อมขึ้นบรรทัดใหม่ 2 ครั้้ง



// ใน 1 ข้อความสามารถเขียนบรรทัดใหม่ได้   โดยใช้ single quoted  ( ' ) หรือขีดเดียว

echo 'You can also have embedded newlines in

strings this way as it is

okay to do<br>';



//  แต่ถ้าใช้ double qouted (") หรือ 2 ขีด  การขึ้นบรรทัดใหม่จะทำตามตัวอย่างข้างล่างนี้ค่ะ

echo "You can also have embedded newlines in ";

echo "strings this way as it is ";

echo "okay to do<br><br>";



// Outputs: Arnold once said: "I'll be back"

echo 'Arnold once said: "I\'ll be back"<br>';

echo"Arnold once said: \"I'll be back\"<br><br>";



// Outputs: You deleted C:\*.*?

echo 'You deleted C:\\*.*?<br>';

echo 'You deleted C:\*.*?<br>';

echo "You deleted C:\\*.*?<br><br>";



// Outputs: This will not expand: \n a newline

echo 'This will not expand: \n a newline<br>';

echo "This will not expand: \\n a newline<br><br>";



// Outputs: Variables do not $expand $either

echo 'Variables do not $expand $either<br>';

echo "Variables do not \$expand \$either<br><br>";

?>




ผลลัพธ์

ทดสอบการแสดงสัญลักษณ์พิเศษ
ทดสอบการแสดงสัญลักษณ์พิเศษ

You can also have embedded newlines in strings this way as it is okay to do
You can also have embedded newlines in strings this way as it is okay to do

Arnold once said: "I'll be back"
Arnold once said: "I'll be back"

You deleted C:\*.*?
You deleted C:\*.*?
You deleted C:\*.*?

This will not expand: \n a newline
This will not expand: \n a newline

Variables do not $expand $either
Variables do not $expand $either


 

 




การต่อสตริง(String) กับ ตัวแปร (Variable)

ตัวอย่าง

Code:


<?

$a = "webmaster";

echo "ฉันเป็น ".$a." www.ban-goi.com";

?>

ผลลัพธ์

ฉันเป็น webmaster www.ban-goi.com



อ้างอิง :
1. เรียนลัด PHP 4 (สมประสงค์ ธิตินิลนิธิ)
2. www.php.net

 

 

Add comment


Security code
Refresh

หน้าหลัก บทเรียน PHP (ผู้เริ่มต้น) PHP - ข้อมูลชนิดอักขระ (String)
Joomla template modify by Ban-Goi.com