Any given web page can have many types of coding: HTML, JavaScript, PHP, MySQL, etc. Each of these coding systems needs quotes in certain places of the code. How do do the quotes is a real puzzle when one line of code may include different coding systems.
Programmers have to understand the effects of the types of quotes (double quotes or single quotes) on the page. Understanding the results leads to a set of strategies to accomplish what the programmer wants to happen on the web page.
SINGLE AND DOUBLE QUOTES IN DIFFERENT LANGUAGES
- HTML properties need double quotes.
Example: <input name="Last" type="text" id="Last" size="40" maxlength="25" value="">
- PHP strings need some kind of quote.
Example 1: echo "This string is in double quotes .";
Example 2: echo 'This string is in single quotes.';
- With double quotes in PHP, you can put variables right in the string, and PHP will substitute the variable value.
The Variable: $AdminUser = "Nora";
Example 1: echo "Welcome $AdminUser!";
Example 2: echo 'Welcome $AdminUser!';
- With single quotes, you can concatenate the variable with the string. (The concatenation variable is period in PHP.)
The Variable: $AdminUser = "Nora";
Example 1: echo 'Welcome $AdminUser!';
Example 2: echo 'Welcome '.$AdminUser.'!';
- MySQL queries need single or double quotes for strings.
Query Example 1: SELECT FirstName, LastName, Phone from Contacts where LastName = "Smith";
Query Example 2: SELECT FirstName, LastName, Phone from Contacts where LastName = 'Smith';
- JavaScript needs single or double quotes on strings.
JS Exmple 1: var Address = "123 Pine Street";
JS Example 2: alert("User Name cannot be blank.");
- Quotes and apostrophes can be escaped with a backslash.
Example: $Message = "The user name you entered, \"$UserName\" is not a valid user name."
If you open a quote, any other quote will close it. If there is a quote in the string that you don't want to close that quote, you can backslash it to have the code just treat it like another character and not a closing quote.
WHAT HAPPENS WHEN WE MIX CODING TYPES?
None of the 6 items above is a problem, if a web page only had one type of coding, but they often don't. PHP can be used to create HTML, MySQL queries and JavaScript.
- Using PHP to create HTML
Example 1: echo "<table><tr><td>This text is in a table</td></tr></table> ";
Single or double quotes could do in this case. There are neither variables or other coding types.
If single quotes are used, the system won't even look to substitute a variable value; so, it may process more quickly with
single quotes.
Example 2: echo '<span class="ErrorMessage">First and Last Names are required fields.</span>';
This example is different because the class needs double quotes. It must use single quotes in the PHP.
Example 3: echo '<span class="RedLinks12">Welcome '.$WebsiteUser.'!</span>';
This example has to use single quotes for the same reason as Example 2; so, the string has to be closed and the variable has to be concatenated.
Example 4: echo '<span class="ErrorMessage">Your entry'.$_POST['Email'].' is not in the right format.</span>';
This example has special problems 1) single quotes have to be used to allow the double quotes for the class, 2) the variable has to be concatenated because of the single quotes, 3) the variable is a superglobal array and the element has single quotes.
Example 5: echo " <span class=\"ErrorMessage\">First and Last Names are required fields.</span>";
This is the same as #2, except that double quotes are used. Then the double quotes for the class are backslashed; so, PHP will not treat them as functional, but just as more characters in the string.
- Using PHP to create JavaScript
Example 1: echo 'var point = new GLatLng('.$ObservationLatitude.', '.$ObservationLongitude.');';
The GLatLng function takes two numbers; so, the string that is echo'd will not need two sets of quotes. This example concatenates the variables, but the string could also be double quoted.
Example 2: $MarkerText = '<span style=\"font-weight: bold; text-align: left;\"> '.$ObservationLatitude.' , '.$ObservationLongitude.'</span>';
This is not actually JavaScript. Instead, the PHP is creating an HTML string that will be passed into the JavaScript. The HTML needs double quotes. The string for the variable definition needs single or double quotes. There are variables.
Example 3: echo 'map.addOverlay(new createMarker(point, '.$i.', "'.$MarkerText.'"));';
- Using PHP to create MySQL
It is very common to use a MySQL database connection with PHP pages and create the queries and put them into a local variable. There queries are commonly used with the values entered in an online form.
EXAMPLE: Create a MySQL Query with a variable The Code
$ContactQuery = "select FirstName, LastName, PhoneNumber from Contacts where FirstName = '$First' and LastName = '$Last';"; $ContactQuery = 'select FirstName, LastName, PhoneNumber from Contacts where FirstName = "'.$First.'" and LastName = "'.$LastName.'";';
Related Information
- State/Province Dropdown with Selected
- Random Image Generator Code - be sure to read the article carefully for the logic error discussion
- When to Use Single and Double Quotes