Câu hỏi phỏng vấn PHP
Câu hỏi

What is the difference between single-quoted and double-quoted strings in PHP?

Câu trả lời

The main differences between single-quoted and double-quoted strings in PHP are:

  1. Variable interpolation: Double-quoted strings allow variables to be interpolated directly within the string, while single-quoted strings treat variables literally and do not evaluate them[1][2][3]. For example:
php Copy
$name = "John";
echo "Hello, $name!"; // Output: Hello, John!
echo 'Hello, $name!'; // Output: Hello, $name!
  1. Escape sequences: Double-quoted strings interpret escape sequences like \n for newline, while single-quoted strings treat escape sequences literally[1][2][3]. For example:
php Copy
echo "Hello\nWorld"; // Output: 
                     //Hello
                     //World
echo 'Hello\nWorld'; // Output: Hello\nWorld
  1. Performance: Single-quoted strings are slightly faster than double-quoted strings because PHP doesn't need to parse variables and escape sequences within them[1][2]. However, this difference is negligible in most cases.

  2. Escaping quotes: To include a single quote ...

junior

junior

Gợi ý câu hỏi phỏng vấn

entry

What is the return type of a function that doesn't return anything?

senior

What is the crucial difference between using traits versus interfaces?

middle

What is autoloading classes in PHP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào