Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use php quote escapes for single quotes or use double quotes in arrays?

Tags:

arrays

php

quotes

I realized that I can have problems with single quotes in php arrays:

<?php
$lang = array(
    'tagline' => 'Let's start your project',
    "h1" => "About Me"
);
?>

So I changed it to double quotes:

<?php
$lang = array(
    "tagline" => "Let's start your project",
    "h1" => "About Me"
);
?>

Should I use "php quote escapes", instead of what I just did? (by the way how to write "php quote escapes"?)

like image 980
alexchenco Avatar asked Feb 23 '10 12:02

alexchenco


People also ask

Should I use single or double quotes in PHP?

In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.

Should I use single or double quotes programming?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

What is the difference between single quotation marks and double quotation marks in PHP?

Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences.

Which is better to use with a print function double quotes or single quotes Why?

# this code prints the output within quotes. The choice between both the types (single quotes and double quotes) depends on the programmer's choice. Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL.


2 Answers

First of all, some people will say that simple-quoted strings are faster that double-quoted strings ; you should not care about that kind of micro-optimization : it will not make any difference for your application.


The difference between simple-quoted and double-quoted strings is :

  • with double-quoted strings, there is variable interpolations
  • with double-quoted strings, you can use some special characters like \n, \t, ...
  • with single-quoted strings, you have simple-quotes to escape.

For reference, in the PHP manual :

  • Single quoted
  • Double quoted


I would that that, in the general matter, it's mostly a matter of personnal preferences...

Personnaly, in a situation such as the one you described, I would use a double-quoted string, like you did : it make the code easier to both write and read, as you don't have to escape that quote.

like image 114
Pascal MARTIN Avatar answered Oct 01 '22 00:10

Pascal MARTIN


Do whatever is most readable, assuming you don't need the double-quote special features. For me, that's using whichever quotes for the string that appear the least in the string.

like image 36
outis Avatar answered Oct 01 '22 00:10

outis