Php: Different Quotes

PHP: different quotes?

Variable-substitution isn't done when using single quotes ('), meaning that the values in your first example would literally be $1 $2 etc if it was a regular string and not passed on to a function that replaces them.

If you don't need variable-substitiution, it's better to stick with single quotes for performance reasons.

`` invokes the shell-engine and invokes it as an actual command, and returning the result, just like in perl. Hence, it has a completely different meaning.

examples:


$email = 'user@example.org';
$sql1 = "SELECT user_id FROM users WHERE email = $email";
$sql2 = 'SELECT user_id FROM users WHERE email = $email';

$sql1 would be SELECT user_id FROM users WHERE email = user@example.org

$sql2 would be SELECT user_id FROM users WHERE email = $email

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

PHP strings can be specified not just in two ways, but in four ways.

  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

PHP using double quotes or single quotes in a function difference

It doesn't really matter what quotes you choose. There are several discussions about performance. Indeed double quotes are a tiny bit slower. But the difference is minimal even at 100,000+ iterations.

The only differences are the concenation and escaping:

Concenation

Double quotes:

$b = 1;
echo "Test ".$b; // outputs Test 1
echo "Test $b"; // also outputs Test 1

Single quotes:

$b = 2;
echo 'Test '.$b; // outputs Test 2
echo 'Test $b'; // outputs Test $b

Escaping

Double quotes:

echo "\t";
// outputs actual tab letter

Single quotes

echo '\t';
// outputs \t as plain text.

single quotes and double quotes in php?

The use of ' and " for strings only changes when they are the outer container of the entire string - a different quote inside that is just treated as a plain character with no special meaning, therefore "'$var'" uses the rules of " (parsing variables), whereas '"$var"' would literally output "$var" as it uses the rules of ' (no parsing of variables).

Summary:
When you do "''" or "\"\"" the quotes inside the string are not parsed by PHP and treated as literal characters, the contents of such quotes will have the same behaviour in or out of those quotes.

How to work with double quotes and single quotes in php

You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:

echo "String with \"double quotes\"";

So in your case it will be:

echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";

Ref: Double quotes within php script echo

Double quotes within php script echo

You need to escape ", so it won't be interpreted as end of string. Use \ to escape it:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";

Read more: strings and escape sequences

When should you use single or double quotes in PHP?

The only difference is that double quoted strings interpret embedded variables and a number of escape sequences, while single quoted strings do not. E.g.:

'This is $a \n string'

This is literally the string "This is $a \n string".

"This is $a \n string"

This is a string containing the value of variable $a and a line break.

Use both as appropriate. If neither escape sequences nor variables are of interest to you, I'd default to single quoted strings; with one exception: if you need single quotes in the string, then double quotes are "cleaner". Compare:

'I don\'t care.'
"I don't care."

How to force either single or double quotes in PhpStorm?

There is no automatic conversion or enforcing double quoted strings over single quoted on code reformat or whatnot.

https://youtrack.jetbrains.com/issue/WI-49520 -- watch this ticket (star/vote/comment) to get notified on any progress. This ticket has been implemented in PhpStorm 2020.3 and the IDE now can detect unnecessary double quotes without an extra plugin.

The conversion between single and double quotes can be done manually via Intentions/Quick Fix menu (see screenshots below) and even in a batch.


Php Inspections (EA Extended) plugin has an Inspection for this (might be disabled by default).

Settings/Preferences | Editor | Inspections | PHP | Php Inspections (EA Extended) | Code Style | Unnecessary double quotes

Sample Image

Once enabled such strings will be marked with Weak Warning severity (which you can change to something more noticeable) with Quick Fix available.

Sample Image

Sample Image

P.S. The "Replace quotes" intention for switching between different quotes (both ways) is also available.

Since it's an Intention, you can batch-run it as well as batch-fix, e.g. by using Code | Run Inspection by Name...

Sample Image

Sample Image



Related Topics



Leave a reply



Submit