Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: different quotes?

Tags:

php

quotes

What is the difference between the quotes " and ' ? What about `? Is there an error in using different quotes ' and " below?

 $result = pg_query_params($dbconn,
      'INSERT INTO users 
      (username, email, passhash_md5)
      VALUES ($1, $2, $3)',
          array($username, $email, $passhash_md5
      )


      $result = pg_query_params( $dbconn,
          "SELECT user_id
           FROM users
          WHERE email = $1",
          array( $email )
          )
like image 881
Léo Léopold Hertz 준영 Avatar asked Aug 23 '09 08:08

Léo Léopold Hertz 준영


People also ask

What does ?: Mean in PHP?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

Can you use single quotes and double quotes for PHP?

If it is a special character, then it will be considered as it is during the string parsing. Strings in PHP can be specified in four different ways: Single Quoted, Double Quoted, Heredoc Syntax and nowdac syntax. The single quoted and double quoted are the most frequently used.

How do you quote in PHP?

A single-quoted string only uses the escape sequences for a single quote or a backslash. Here are some common escape sequences for double-quoted strings: \" for a double quote. \\ for a backslash.

Can you use double quotes 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.


1 Answers

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 = '[email protected]';
$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 = [email protected]

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

like image 80
jishi Avatar answered Oct 11 '22 19:10

jishi