PHP - Escape Character

จิดาภา ก้อนดิน 620710651

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
ทำหน้าที่

\'

\'

\'

\'

Single Quote

เครื่องหมายคำพูดเดี่ยว

\"

\"

\"

Double Quote

เครื่องหมายคำพูดคู่

\$

PHP variables

แสดงเครื่องหมาย $(ไม่ใช่ตัวแปร)

\n

\n

\n

\n

New Line

ขึ้นบรรทัดใหม่

\r

\r

\r

\r

Carriage Return

เริ่มต้นบรรทัดใหม่

\t

\t

\t

\t

Tab

การแท็บ

\f

\f

\f

\f

Form Feed

ใช้ในการเลื่อนหน้า

\ooo

\ooo

\ooo

Octal value

ใช้เพื่อแทนค่าเลขฐานแปด

\xhh

\xhh

\xhh

Hex value

ใช้เพื่อแทนค่าเลขฐานสิบหก

\0

\0

null

ไม่มีค่า

\a

\a

Alarm/Beep

เสียงเตือน

Reference

  • ใช้ศึกษาความหมายและตัวอย่างคำสั่งและใช้เขียนตาราง Escape Character

W3Schools. (n.d.). PHP - escape characters. W3Schools. https://www.w3schools.com/php/php_string_escape.asparrow-up-right

  • ใช้ศึกษาความหมายเพิ่มเติมของ Escape Character และความหมายของแต่ละสัญลักษณ์

PHP. (n.d.). Escape sequences. PHP. https://www.php.net/manual/en/regexp.reference.escape.phparrow-up-right

  • ใช้ศึกษา Escape Character ของภาษา C

Javatpoint. (n.d.). Escape sequence in C. Javatpoint. https://www.javatpoint.com/escape-sequence-in-carrow-up-right

  • ใช้ศึกษา Escape Character ของภาษา C และตัวอย่างของภาษา C

GeeksforGeeks. (n.d.). Escape sequence in C. GeeksforGeeks. https://www.geeksforgeeks.org/escape-sequence-in-c/arrow-up-right

  • ใช้ศึกษา Escape Character ของภาษา Java

Javatpoint. (n.d.). Java escape characters. Javatpoint. https://www.javatpoint.com/java-escape-charactersarrow-up-right

  • ใช้ศึกษา Escape Character ของภาษา Java และตัวอย่างของภาษา Java

Banerjee, P. (2023, November 1). Escape sequences in Java. GeeksforGeeks. https://www.geeksforgeeks.org/escape-sequences-in-java/arrow-up-right

  • ใช้ศึกษา Escape Character ของภาษา Pythonและตัวอย่างของภาษา Python

W3Schools. (n.d.). Python escape characters. W3Schools. https://www.w3schools.com/python/gloss_python_escape_characters.asparrow-up-right

Video Presentation

Slide

file-pdf
437KB

Last updated