What is the difference between single and double quotes in php. What is the difference between single and double quoted strings in PHP? String type implementation details

It's no secret that quotes in PHP can be single or double. Let's find out when certain uses of quotation marks are more appropriate.

Single quotes

The simplest way to define a string is to enclose text in single quotes.

If we need to use a single quote in text, we need to escape it with a forward slash (\).

Escape sequences within single quotes do not work.

Examples of using single quotes:

Double quotes

If you highlight text with double quotes, the string will be defined in the same way as with single quotes. But of course there are differences between quotes.

The situation with escaping quotes is the same as with single quotes.

A string that is enclosed in double quotes recognizes most escape sequences for special characters.

The most important difference is the fact that double quotes handle variables.

Examples of using double quotes:

Let's focus on the fact that strings with double quotes handle variables.

What happens when a string is processed? Interpreter check each string with double quotes for variables, i.e. forced parsing occurs, which takes additional time. Yes, often this is a split second, but the fact itself must be understood. That is, if you compare the processing of one string with different quotes (without variables, of course), then a string with single quotes will definitely be processed faster.

Calculations

On the ProfiPHP website I found interesting calculations for this topic. The author wrote a simple script with which he calculated the time to process strings.

This short article shows how and where to use quotes in PHP.

Single quotes (apostrophes) in PHP

Strings enclosed in single quotes are not processed in any way by PHP. That is, single quotes represent the text enclosed between them as is.

// Correct echo "How's life?"; echo "How's life? $name"; echo "How's life?".$name; // Incorrect echo "How are you? $name";

Special characters in single and double quotes

To ensure that, for example, the tab character (\t) is interpreted as a tab character rather than as a slash and the letter t, you must enclose the line of text that contains the tab character in double quotes. You can only use \' and \\ in single quotes. All other escape sequences (\n, \r, \$, etc.) are not allowed within single quotes.

// Incorrect echo "How are you?\n"; // Correct echo "How are you?\n";

To escape double quotes within a string, place the quotes before the backslash \" .

// Incorrect echo "

What's up?

"; // Correct echo "

What's up?

"; echo "

What's up?

";

Double quotes in PHP

Text enclosed in double quotes is treated very differently. For example, variables enclosed in double quotes are replaced with their values. This makes it convenient to compile SQL queries using double quotes.

$query = "INSERT INTO table (post,author,text,date) VALUES ("$id","$author","$text","$date"");

I'm not an expert in PHP programming, but I'm a little confused why I see some code in PHP with a string enclosed in single and sometimes double quotes.

I just know that in .NET or C language, if it's in single quotes, it means it's a character and not a string.

Solution

What you should know

$a = "name"; $b = "my $a"; == "my name" $c = "my $a"; != "my name"

In PHP, people use single quotes to define a constant string like "a" , "my name" , "abc xyz" while using double quote to define a string containing an identifier like "a $b $c $d" .

And another thing is

Echo "my name";

faster than

Echo "my name";

Echo "my" . $a;

slower than

Echo "my $a";

This is true for other strings used.

In PHP, text in single quotes is treated as a string value, while text in double quotes will parse variables by replacing and processing their value.

$test = "variable"; echo "Hello Mr $test"; // the output would be: Hello Mr variable echo "Hello Mr $test"; // the output would be: Hello Mr $test

Here the double quote parses the value and the single quote is treated as a string value (without parsing the $test variable.)

Both kinds of nested characters are strings. It is convenient to use one type of quotation to conclude another type of quotation. """ and """ . The biggest difference between quote types is that nested identifier references are replaced inside double quotes rather than inside single quotes.

The simplest way to define a string is to enclose it in single quotes (the symbol " ).

To use a single quote inside a string, escape it with a backslash ( \ ). If you need to write the backslash itself, duplicate it ( \\ ). All other uses of backslashes will be interpreted as normal characters: this means that if you try to use other escape sequences such as \r or \n, they will be output as is instead of any special behavior.

echo "this is a simple string";

echo "You can also insert into lines
newline character like this,
This is fine"
;

// Outputs: Arnold once said: "I"ll be back"
echo "One day Arnold said, 'I\"ll be back.'';

Echo "Did you delete C:\\*.*?";

// Outputs: Did you delete C:\*.*?
echo "Did you delete C:\*.*?" ;

// Outputs: This will not be expanded: \n newline
echo "This will not be expanded: \n newline";

// Outputs: $expand and $either variables are not expanded
echo "$expand and $either variables are not expanded";
?>

Double quotes

If the string is enclosed in double quotes ("), PHP recognizes more escape sequences for special characters:

Escape Sequences
Subsequence Meaning
\n newline (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\e escape character (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4)
\f page feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double quote
\{1,3} sequence of characters corresponding to a regular expression of a character in octal number system
\x(1,2) sequence of characters corresponding to the regular expression of a character in hexadecimal notation

As with a string enclosed in single quotes, escaping any character will also output the backslash itself. Before PHP 5.1.1, backslash in \($var) was not published.

Heredoc

The third way to define strings is to use heredoc syntax: <<< . After this operator, you must specify an identifier, then a line feed. After this comes the line itself, and then the same identifier, closing the insertion.

Line must start with a closing identifier, i.e. it must appear in the first column of the row. Additionally, the identifier must follow the same naming rules as all other tags in PHP: contain only alphanumeric characters and an underscore, and must not start with a number (underscores are allowed).

Attention

It is very important to note that the closing identifier line must not contain any other characters except a semicolon ( ; ). This means that the id should not be indented and that there cannot be any spaces or tabs before or after the semicolon. It is also important to understand that the first character before the closing identifier must be a newline character as defined by your operating system. For example, on UNIX systems, including Mac OS X, this \n. A new line must also begin immediately after the closing identifier.

If this rule is violated and the closing identifier is not "clean", the closing identifier is considered missing and PHP will continue to look for it further. If in this case the correct closing identifier is never found, it will cause a parsing error with the line number at the end of the script.

Heredoc cannot be used to initialize class fields. Starting from PHP 5.3, this restriction only applies to heredocs containing variables.

Example #1 Wrong example

class foo (
public $bar =<<bar
EOT;
}
?>

Heredoc text behaves in the same way as a string in double quotes, without having them. This means that you don't need to escape quotes in heredoc, but you can still use the escape sequences above. Variables are processed, but you need to be as careful when using complex variables inside heredoc as when working with strings.

Example #2 Example of defining a heredoc string

$str =<<Example line,
covering several lines,
using heredoc syntax.
EOD;

Class foo
{
var $foo ;
var $bar ;

Function foo()
{
$this -> foo = "Foo" ;
$this ->
}
}

$foo = new foo();
$name = "MyName" ;

echo<<My name is "$name". I type $foo -> foo .
Now I'm deducing
( $foo -> bar [ 1 ]) .
This should output the capital letter "A": \x41
EOT;
?>

My name is "MyName". I type Foo. Now, I output Bar2. This should output a capital letter "A": A

It is also possible to use heredoc syntax to pass data through function arguments:

Since version 5.3.0, it has become possible to initialize static variables and class properties/constants using heredoc syntax:

Example #4 Using heredoc to initialize static variables

// Static variables
function foo()
{
static $bar =<<There's nothing here...
LABEL;
}

// Class properties/constants
class foo
{
const BAR =<<Example of using a constant
FOOBAR;

Public $baz =<<Example of using a field
FOOBAR;
}
?>

As of PHP 5.3.0, you can also surround the Heredoc identifier with double quotes:

Nowdoc

Nowdoc is the same for single-quoted strings as heredoc is for double-quoted strings. Nowdoc is similar to heredoc, but inside it no substitutions are made. This design is ideal for embedding PHP code or other large blocks of text without having to escape it. In this it is a bit similar to the SGML construct by declaring a block of text not intended to be processed.

Nowdoc is indicated by the same sequence <<< , which is used in heredoc, but the following identifier is enclosed in single quotes, for example, <<<"EOT" . All conditions that apply to heredoc identifiers also apply to nowdoc, especially those that apply to the closing identifier.

Example #6 Nowdoc example

$str =<<<"EOD"
Example text,
spanning several lines
using nowdoc syntax.
EOD;

/* More complex example with variables. */
class foo
{
public $foo ;
public $bar ;

Function foo()
{
$this -> foo = "Foo" ;
$this -> bar = array("Bar1" , "Bar2" , "Bar3" );
}
}

$foo = new foo();
$name = "MyName" ;

echo<<<"EOT"
My name is "$name". I print $foo->foo.
Now I print ($foo->bar).
This should not output a capital "A": \x41
EOT;
?>

The result of running this example:

My name is "$name". I print $foo->foo. Now I print ($foo->bar). This should not output a capital "A": \x41

Comment:

Unlike heredoc, nowdoc can be used in any context with static data. A typical example of initializing class fields or constants:

Example #7 Example of using static data

class foo (
public $bar =<<<"EOT"
bar
EOT;
}
?>

Comment:

nowdoc support was added in PHP 5.3.0.

Handling Variables

If a string is specified in double quotes, or using heredoc, the variables inside it are processed.

There are two types of syntax: simple and complex. Simple syntax is easier and more convenient. It makes it possible to process a variable, an array value ( array) or object properties ( object) with a minimum of effort.

Complex syntax can be identified by the curly braces surrounding the expression.

Simple syntax

If the interpreter encounters a dollar sign ( $ ), it captures as many characters as possible to form a valid variable name. If you want to specify the end of a name, enclose the variable name in curly braces.

$juice = "apple" ;

echo "He drank some $juice juice." . PHP_EOL ;
// doesn't work, "s" is a valid character for a variable name,
// but our variable is named $juice.
echo "He drank some juice made of $juices." ;
?>

The result of running this example:

He drank some apple juice. He drank some juice made of .

An array element ( array) or object property ( object). In array indices there is a closing square bracket ( ] ) marks the end of the index definition. The same rules apply to object properties as to simple variables.

Example #8 Simple syntax example

$juices = array("apple" , "orange" , "koolaid1" => "purple" );

echo "He drank some $juices [ 0 ] juice." . PHP_EOL ;
echo "He drank some $juices [ 1 ] juice." . PHP_EOL ;
echo "He drank some $juices [ koolaid1 ] juice." . PHP_EOL ;

class people (
public $john = "John Smith" ;
public $jane = "Jane Smith" ;
public $robert = "Robert Paulsen" ;

Public $smith = "Smith" ;
}

$people = new people();

echo "$people -> john drank some $juices [ 0 ] juice." . PHP_EOL ;
echo " $people -> john then said hello to $people -> jane ." . PHP_EOL ;
echo "$people -> john "s wife greeted $people -> robert." . PHP_EOL;
echo " $people -> robert greeted the two $people -> smiths ." ; // Won't work
?>

The result of running this example:

He drank some apple juice. He drank some orange juice. He drank some purple juice. John Smith drank some apple juice. John Smith then said hello to Jane Smith. John Smith's wife greeted Robert Paulsen. Robert Paulsen greeted the two.

For anything more complex, use complex syntax.

Complex (curly) syntax

It is called complex not because it is difficult to understand, but because it allows the use of complex expressions.

Any scalar variable, array element, or object property mapped to a string can be represented in a string using this syntax. Just write the expression the same way you would outside the line and then wrap it in { And } . Because the { cannot be escaped, this syntax will only be recognized when $ follows directly { . Use {\$ to print {$ . A few illustrative examples:

// Show all errors
error_reporting(E_ALL);

$great = "great" ;

// Doesn't work, outputs: This is (great)
echo "This is ( $great )" ;

// Works, outputs: This is great
echo "This is ( $great ) " ;
echo "This is $( great ) " ;

// Works
echo "This square is wide( $square -> width ) 00 centimeters." ;

// Works, quoted keywords only work with curly brace syntax
echo "This works: ( $arr [ "key" ]) " ;

// Works
echo "This works: ( $arr [ 4 ][ 3 ]) " ;

// This is invalid for the same reason as $foo outside
// lines. In other words, it will still work,
// but since PHP looks for the constant foo first, this will cause
// level error E_NOTICE (undefined constant).
echo "It is not right:( $arr [ foo ][ 3 ]) " ;

// Works. When using multidimensional arrays internally
// lines always use curly braces
echo "This works: ( $arr [ "foo" ][ 3 ]) " ;

// Works.
echo "This works: " . $arr [ "foo" ][ 3 ];

echo "This works too:( $obj -> values ​​[ 3 ]-> name ) " ;

echo "This is the value of the variable named$name : ($( $name )) " ;

echo "This is the value of the variable name that getName() returns:($( getName ())) " ;

echo "This is the value of the variable by name that \$object->getName() returns:($( $object -> getName ())) " ;

// Doesn't work, outputs: This is what getName() returns: (getName())
echo "This is what getName() returns: (getName())";
?>

It is also possible to access object properties within strings using this syntax.

class foo (
var $bar = "I am bar." ;
}

$foo = new foo();
$bar = "bar" ;
$baz = array("foo" , "bar" , "baz" , "quux" );
echo " ( $foo -> $bar ) \n" ;
echo " ( $foo -> $baz [ 1 ]) \n" ;
?>

The result of running this example:

I am bar. I am bar.

Comment:

Functions, method calls, static class variables, and class constants work internally {$} , starting with PHP 5. However, the supplied value will be treated as a variable name in the same context as the line in which it is defined. Using single curly braces ( {} ) will not work for accessing the values ​​of functions, methods, class constants, or static class variables.

// Show all errors
error_reporting(E_ALL);

class beers (
const softdrink = "rootbeer" ;
public static $ale = "ipa" ;
}

$rootbeer = "A & W" ;
$ipa = "Alexander Keith\"s" ;

// This works, outputs: I would like A & W
echo "I'd like ($( beers :: softdrink )) \n" ;

// This works too, outputs: I would like Alexander Keith's
echo "I'd like ($( beers :: $ale )) \n" ;
?>

Accessing and changing a character in a string

Characters in strings can be used and modified by specifying their offset from the beginning of the string, starting at zero, in square brackets after the string, for example, $str . Think of a string for this purpose as an array of characters. If you need to get or replace more than 1 character, you can use the functions substr() And substr_replace().

Comment: A character in a string can also be accessed using curly braces, for example $str(42) .

Attention

Attempting to write to an offset beyond the line's boundaries will pad the string with spaces up to that offset. Non-integer types will be converted to integer types. The wrong offset type will cause a level error E_NOTICE. Writing to a negative offset will cause a level error E_NOTICE, and when read it will return an empty string. Only the first character of the assigned string is used. Assigning to an empty string assigns a null byte (NULL).

Attention

Strings in PHP are internally arrays of bytes. As a result, accessing or modifying a string at an offset is not multi-byte encoding safe, and should only be done with strings in single-byte encodings, such as ISO-8859-1.

Example #9 Some example strings

// Get the first character of the string
$str = "This is a test." ;
$first = $str [ 0 ];

// Get the third character of the string
$third = $str [ 2 ];

// Get the last character of the string
$str = "This is still a test." ;
$last = $str [ strlen ($str ) - 1 ];

// Change the last character of the line
$str = "Look at the sea" ;
$str [ strlen ($str )- 1 ] = "e" ;

?>

As of PHP 5.4, the offset in the string must be specified as either an integer or a string containing digits, otherwise a warning will be issued. Previously offset given by a string like "foo", without warning was transformed into 0 .

Example #10 Differences between PHP 5.3 and PHP 5.4

$str = "abc" ;

Var_dump($str["1"]);
var_dump (isset($str [ "1" ]));

Var_dump($str["1.0"]);
var_dump (isset($str [ "1.0" ]));

Var_dump($str["x"]);
var_dump (isset($str [ "x" ]));

Var_dump($str["1x"]);
var_dump (isset($str [ "1x" ]));
?>

The result of running this example in PHP 5.3:

string(1) "b" bool(true) string(1) "b" bool(true) string(1) "a" bool(true) string(1) "b" bool(true)

The result of running this example in PHP 5.4:

string(1) "b" bool(true) Warning: Illegal string offset "1.0" in /tmp/t.php on line 7 string(1) "b" bool(false) Warning: Illegal string offset "x" in / tmp/t.php on line 9 string(1) "a" bool(false) string(1) "b" bool(false)

Comment:

Trying to access variables of other types (excluding arrays or objects that implement certain interfaces) using or {} will silently return NULL.

Comment:

PHP 5.5 added support for accessing characters in string literals using the syntax or {} .

There are many useful functions for modifying strings.

Basic functions are described in the section on string functions, and for advanced search and replace, regular expression or Perl-compatible regular expression functions.

Convert to string

The value can be converted to a string using a cast (string), or functions strval(). In expressions where a string is required, the conversion occurs automatically. This happens when you use functions echo or print, or when the value of a variable is compared with a string. Reading the manual's Types and Type Manipulation sections will make the following clearer. see also settype().

Arrays are always converted to string "Array", so you can't display the contents of the array ( array), using echo or print to see what it contains. To view a single element, use something like echo $arr["foo"]. See below for tips on how to display/view all content.

Objects in PHP 4 were always converted to a string "Object". If you want to display the values ​​of an object's fields ( object) for debugging purposes, read on. If you want to get the class name of the required object, use get_class(). Since PHP 5, the __toString method has also become available.

NULL is always converted to the empty string.

As you can see above, directly converting arrays, objects, or resources to a string does not provide any useful information about the values ​​themselves other than their types. A better way to output values ​​for debugging is to use functions print_r() And var_dump().

Most values ​​in PHP can be converted to a string for persistent storage. This method is called serialization and can be done using the function serialize(). Additionally, if your PHP installation has WDDX support, serialization to an XML structure is also possible.

Converting strings to numbers

If the string is recognized as a numeric value, the resulting value and type are determined as follows.

If the string does not contain any of the characters ".", "e", or "E", and the value of the number falls within the limits of integers (defined PHP_INT_MAX), the string will be recognized as an integer ( integer). In all other cases it is considered a floating point number ( float).

The value is determined by the beginning of the string. If the line starts with a valid numeric value, that value will be used. Otherwise the value will be 0 (zero). A valid numeric value is one or more digits (which may contain a decimal point), optionally preceded by a sign, followed by an optional exponent. The exponent is "e" or "E" followed by one or more digits.

$foo = 1 + "10.5" ; // $foo is a float (11.5)
$foo = 1 + "-1.3e3" ; // $foo is a float (-1299)
$foo = 1 + "bob-1.3e3" ; // $foo is an integer (1)
$foo = 1 + "bob3" ; // $foo is an integer (1)
$foo = 1 + "10 Small Pigs" ; // $foo is an integer (11)
$foo = 4 + "10.2 Little Piggies" ; // $foo is a float (14.2)
$foo = "10.0 pigs " + 1 ; // $foo is float (11)
$foo = "10.0 pigs " + 1.0 ; // $foo is float (11)
?>

More detailed information For information about this conversion, see the section on strtod(3) in the Unix documentation.

If you want to test any of the examples in this section, copy and paste it and the following line to see what happens:

echo "\$foo== $foo ; type: " . gettype ($foo) . "
\n" ;
?>

Don't expect to get the code of a character by converting it to an integer (as is done, for example, in C). To convert characters to their ASCII codes and back, use the functions ord() And chr().

String type implementation details

String type ( string) in PHP is implemented as an array of bytes and an integer containing the length of the buffer. It does not contain any information about how to convert these bytes into characters, leaving this task to the programmer. There are no restrictions on the contents of a string, such as a byte with a value 0 ("NUL" byte) can be located anywhere (however, be aware that some functions, as stated in this manual, are not "binary safe", i.e. they can pass strings to libraries that ignore data after NUL -byte).

This nature of the string type explains why PHP does not have a separate “byte” type - strings play that role. Functions that return non-text data—for example, an arbitrary data stream read from a network socket—still return strings.

Considering the fact that PHP does not dictate a specific encoding for strings, one might ask how string literals are then encoded. For example, the line "á" equivalent "\xE1"(ISO-8859-1), "\xC3\xA1"(UTF-8, normalization form C), "\x61\xCC\x81"(UTF-8, normalization form D) or some other possible representation? The answer is that the string will be encoded the way it is written in the script file. Thus, if the script is written in ISO-8859-1 encoding, then the string will be encoded in ISO-8859-1, etc. However, this rule does not apply when Zend Multibyte mode is enabled: in this case, the script can be written in any encoding (either explicitly specified or automatically determined), and then converted to a specific internal encoding, which will subsequently be used for string literals. Please note that the script encoding (or internal encoding if Zend Multibyte is enabled) has some restrictions: the encoding must almost always be a superset of ASCII, such as UTF-8 or ISO-8859-1. Note also that state-dependent encodings, where the same byte values ​​can be used in the initial and non-initial shift state, can cause problems.

Of course, to be useful, string functions must make some assumptions about the encoding of the string. Unfortunately, among PHP functions there is quite a wide variety of approaches to this issue:

  • Some functions assume that the string is encoded in some single-byte encoding, but they do not need to interpret the bytes as specific characters to work correctly. This category includes, for example, substr(), strpos(), strlen() And strcmp(). Another way of thinking about these functions is that they operate on memory buffers, i.e. they work directly with bytes and their offsets. offsets.
  • Other functions expect an encoding to be passed as a parameter, perhaps assuming some default encoding if an encoding parameter was not specified. This function is
  • Finally, there are functions that assume that a string uses a specific encoding, usually UTF-8. Most functions from the intl and PCRE extensions fall here (in the latter case, only when specifying the modifier u). Although this is done on purpose, the function utf8_decode() implies UTF-8 encoding, and utf8_encode()- ISO-8859-1.

Ultimately, writing correct programs that work with Unicode means carefully avoiding functions that don't work with Unicode and are likely to corrupt data, and using valid functions instead, usually from the intl and mbstring extensions. However, using Unicode-capable functions is a good start. Regardless of the features that a language provides, it is necessary to know the Unicode specification itself. For example, if a program assumes that only lowercase and uppercase letters exist in a language, then it is making a big mistake.

What type of quotes should I use to format strings - apostrophes or classic double quotes?

Let's look at the difference between double and single quotes in PHP, and use examples to find out when to use which.

Variables and escape sequences for special characters found in strings enclosed in single quotes are not processed. Strings surrounded by apostrophes are processed much faster by the PHP interpreter than similar strings surrounded by double quotes.

The reason here is simple: the PHP interpreter additionally checks strings in double quotes for the presence of variables, and if they are found, then instead of the variable name, its value is inserted into the string. But a line enclosed in apostrophes is perceived by the interpreter as regular text and PHP does not carry out any transformations in these lines. I think it’s clear that processing strings in single quotes will be faster in any case.

First, let's describe how to define a string, and then check how much faster the processing of strings in single quotes will be.

The simplest way to define a string is to enclose it in single quotes ("). To use single quotes within a single-quoted string, they must be preceded by a backslash (\), that is, escaped. If the backslash must come before a single quote or be at the end of the line, you need to duplicate it.If you try to escape any other character, the backslash will also be printed.

Here's an example of using single quotes:
// Output: Simple string
echo "Simple string";
// Prints: I"m here
echo "I\"m here";
// Output: This will not insert: \n new line
echo "This will not insert:\nnewline";
// Outputs: The $example variable will not be substituted either
echo "The $example variable will not be substituted either"; If the string is enclosed in double quotes ("), PHP recognizes large quantity control sequences for special characters and also substitutes its value instead of the variable name in the string. Just as with single quotes, in order to use double quotes within a double-quoted string, they must be preceded by a backslash character (\).

Here's an example of using double quotes:
// Output: Simple string
echo "Simple string";
// Outputs: Company "Snowdrop"
echo "Company \"Snowdrop\"";
// Output: This will lead to a new line
echo "This will break to a new line \n";
// Output: The variable will be substituted
$example = "will be substituted";
echo "Variable $example"; It should also be remembered that the sequence "\n" ( new line), "\r" (carriage return) for plain text, not HTML. So you will not see changes in the browser (only in source code pages).

Let's find out how much faster single quotes are than double quotes. For measurements, we will write a short test script, and we will immediately note that if you test it yourself, the results, which depend on the hardware of your PC or server, will be different.
// Return the timestamp at the beginning of the loop
$start = microtime(true);
// Create a loop for 1 million iterations
for ($i = 0; $i< 1000000; $i++) {
$text = "Here is a character string";
}
// Calculate the time spent
$time = (microtime(true) - $start); Result: 0.09 seconds.

If we replace single quotes with double quotes:
$text = "Here is a character string"; The result will be 0.10 seconds.

As you can see, when using text strings, the difference in execution time is very small, one might even say it doesn’t exist at all. The fun begins when we try to combine a string and a variable.
$text = "Here is the character string $i"; or
$text = $i."Here is a character string"; Result approximately: 0.27 seconds.

The difference is quite noticeable. Concatenation and double quotes clearly affect performance when variables are added to the string.

When the server processes the code, it checks all the contents of double quotes for variables, constants, and more. It takes time. And the server processes what is between the single quotes as ready-made text and does not care what is there. The difference between the performance of single and double quotes is very small, but if you are developing a highly loaded project, then a few milliseconds saved is already a victory.