PHP: different quotes?

后端 未结 3 1237
太阳男子
太阳男子 2020-11-28 15:47

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

 $result = pg_query_params($         


        
相关标签:
3条回答
  • 2020-11-28 16:37

    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 = 'user@example.org';
    $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 = user@example.org

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

    0 讨论(0)
  • 2020-11-28 16:38

    The difference between single and double quoted strings is well explained in the PHP manual about Strings.

    In your example, since you are using substitution variables such as $1 that mean something specific to pg_query_params and that you do not want PHP to interpret as variable names, you should use single quotes for your SQL query strings.

    0 讨论(0)
  • 2020-11-28 16:41

    Basically, " lets you embed variables like so:

    <?php
    $beer = 'Heineken';
    echo "$beer's taste is great"; // works; "'" is an invalid character for variable names
    echo "He drank some $beers";   // won't work; 's' is a valid character for variable names but the variable is "$beer"
    echo "He drank some ${beer}s"; // works
    echo "He drank some {$beer}s"; // works
    ?>
    

    (From the php manual)

    Using ' means that no checking for variables is done.

    <?php
    echo '$beer';
    ?>
    

    Would output $beer.

    0 讨论(0)
提交回复
热议问题