P粉4587250402023-08-24 13:14:41
Things are evaluated in double quotes, but not in single quotes:
$s = "dollars"; echo 'This costs a lot of $s.'; // This costs a lot of $s. echo "This costs a lot of $s."; // This costs a lot of dollars.
P粉3233748782023-08-24 10:55:52
You can specify
PHP stringsnot just two ways, but four ways.
\'
, and to display a backslash you can use another backslash \\ code> (so yes, even single quoted strings will be parsed ).
$type
and you want to echo "The $types are"
. This will look for the variable $types
. To resolve this issue, use echo "The {$type} are"
. Take a look at String Parsing to learn how to use array variables etc. <<<<<<
. After the operator, provide an identifier and then a newline character. Next is the string itself, and then the same identifier is used again to end the reference. You don't need to escape quotes in this syntax. <<<<<<
sequence as the document here, but the following identifier is enclosed in single quotes, such as <<<'EOT'<<<'EOT'
. No parsing is done in nowdoc. Comments: Single quotes within single quotes and double quotes within double quotes must be escaped:
$string = 'He said "What\'s up?"'; $string = "He said \"What's up?\"";
speed:
no difference.
Please read Trusted Article A PHP core developer talks about this issue. When it comes to tests, we should never take them for granted. It is important to understand that writing trustworthy tests and especially interpreting their results requires a great deal of knowledge and experience. This means that most tests are fake. For example, in code like this
for($i=0;$i<100000;$i++) { 'string'; }
Quoted strings are parsed only once along with the entire script and then converted to opcodes. Then it is executed a million times. So it measures anything except parsing. This is just the tip of the iceberg. With such nanobenchmarks, it's nearly impossible to create a reliable test that won't be corrupted by some disruptive side effects.