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

senior

What's better at freeing memory with PHP: unset() or $var = null ?

senior

What exactly are late static bindings in PHP?

junior

What are the differences between die() and exit() functions in PHP?

Bình luận

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

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