Single Quotes and Double Quotes are Very Different

Single Quotes and Double Quotes are Very Different

Using ” (double quotes) is efficient when programming with PHP. We can always use ‘ (single quotes) unless the necessity

of ” (double quotes). We might think it’s much easier to write code as:

echo “Today is the $day of $month”;

However, using single quotes forces variables to be outside the quotes; instead, we must use the period (.) to combine strings. It makes for faster coding but can be more difficult for other programmers to read. Let’s look at what would happen if we put an associative array value in the previous code:

echo “Today is the $date[‘day’] of $date[‘month’]”;

We would receive a parse error and it would be harder for another team member to read. Two correct ways to write that line of code would be:

echo ‘Today is the ‘ . $date[‘day’] . ‘ of ‘ . $date[’month’];

and

echo “Today is the {$date[’day’]} of {$date[’month’]}”;

These might not look as pretty as the original code, but syntactically they are both correct. Additionally, single quotes is easier to read.

The use of single and double quotes also applies to associative arrays. Consider this code:

$SESSION[new_hash] = $SESSION[”hash”];

One main problem exists in that line of code. The associative entry team on the left side needs to have single quotes around it; otherwise, PHP will think it’s a define and give a warning message (only if error reporting is at maximum). So we can use the above code should look like this:

$SESSION[’new_hash’] = $SESSION[’hash’];

This is the basic difference between single and double quotes.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.