Single Quotes versus Double Quotes


When you put something in “double” quotes, you are asking PHP to check that content for a variable. So even though the following lines do not contain variables within the double quotes, PHP will still waste precious computing time scanning those lines of code.

$str = "Hello World";
if ($str == "Hello World") 
{
    echo "Bye friends"; 
}

Those same three lines of code could be executed much faster if ‘single’ quotes were used in place of “double” quotes.

$str = "Hello World";
if ($str == "Hello World") 
{
    echo "Bye friends"; 
}

PHP will not read a variable if it is in ‘single’ quotes.

echo '$str, Good morning.';
// output: $str, Good morning.
echo "$str, Good morning.";
// Will output: Hello World, Good morning.

Always try to avoid double quotes in your scripts. If you are working with a variable, it is more efficient way for PHP to execute this:

echo $str. 'Good morning.';
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s