Escape Characters คือเครื่องหมาย backslash(\) ตามด้วยตัวอักขระที่ต้องการแทรก หรือใช้แทรกตัวอักษรพิเศษที่ไม่สามารถใช้งานได้โดยตรงในสตริง ตัวอย่างเช่น
$x = "We are the so-called "Vikings" from the north.";
ผลลัพธ์
PHP Parse error: syntax error, unexpected 'Vikings' (T_STRING) in /home/gA3sDb/prog.php on line 6
สามารถแก้ปัญหาได้ด้วยการใช้ \" ทำให้สามารถเขียน " ได้โดยไม่ error เช่น
$x = "We are the so-called \"Vikings\" from the north.";
ผลลัพธ์
We are the so-called "Vikings" from the north
PHP รับรองการแปลงเลขฐานแปด(Octal)และเลขฐานสิบหก(Hexadecimal) เป็นอักขระ ASCII ได้ ตัวอย่างเช่น
<?php
$str = "\120\110\120"; //เลขฐานแปด 120 = P ,110 = H
echo "PHP with Octal: ". $str;
echo PHP_EOL;
$str = "\x50\x48\x50"; //เลขฐานสิบหก x50 = P , x48 = H
echo "PHP with Hexadecimal: ". $str;
?>
ผลลัพธ์
PHP with Octal: PHP
PHP with Hexadecimal: PHP
เราจะทำการเปรียบเทียบการใช้ Escape Characters ของแต่ละภาษา โดยจะให้ภาษา C , Java , Python ในการเปรียบเทียบ ดังนี้
ผลลัพธ์
ยกตัวอย่าง Escape characters ที่ใช้ใน PHP และภาษาอื่น มีดังนี้
PHP
C
Java
Python
Result
ทำหน้าที่
แสดงเครื่องหมาย $(ไม่ใช่ตัวแปร)
ใช้เพื่อแทนค่าเลขฐานสิบหก
ใช้ศึกษาความหมายและตัวอย่างคำสั่งและใช้เขียนตาราง Escape Character
W3Schools. (n.d.). PHP - escape characters. W3Schools. https://www.w3schools.com/php/php_string_escape.asp
ใช้ศึกษาความหมายเพิ่มเติมของ Escape Character และความหมายของแต่ละสัญลักษณ์
PHP. (n.d.). Escape sequences. PHP. https://www.php.net/manual/en/regexp.reference.escape.php
ใช้ศึกษา Escape Character ของภาษา C
Javatpoint. (n.d.). Escape sequence in C. Javatpoint. https://www.javatpoint.com/escape-sequence-in-c
ใช้ศึกษา Escape Character ของภาษา C และตัวอย่างของภาษา C
GeeksforGeeks. (n.d.). Escape sequence in C. GeeksforGeeks. https://www.geeksforgeeks.org/escape-sequence-in-c/
ใช้ศึกษา Escape Character ของภาษา Java
Javatpoint. (n.d.). Java escape characters. Javatpoint. https://www.javatpoint.com/java-escape-characters
ใช้ศึกษา Escape Character ของภาษา Java และตัวอย่างของภาษา Java
Banerjee, P. (2023, November 1). Escape sequences in Java. GeeksforGeeks. https://www.geeksforgeeks.org/escape-sequences-in-java/
ใช้ศึกษา Escape Character ของภาษา Pythonและตัวอย่างของภาษา Python
W3Schools. (n.d.). Python escape characters. W3Schools. https://www.w3schools.com/python/gloss_python_escape_characters.asp
Video Presentation