LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using single quotes vs double quotes in PHP strings (https://www.linuxquestions.org/questions/programming-9/using-single-quotes-vs-double-quotes-in-php-strings-342130/)

vharishankar 07-11-2005 08:44 AM

Using single quotes vs double quotes in PHP strings
 
I've read somethings on using single quotes and double quotes in PHP strings and I always use single quotes to enclose all my strings.

I was just curious as to why some people use single quotes and why some use double quotes in PHP strings.

Is it a matter of preference, convenience or is there any advantage to using one over the other?

slackie1000 07-11-2005 09:14 AM

hi there,
makes difference for some special cases...
here you will find a better explanation...
regards
slackie1000

Hko 07-11-2005 10:11 AM

Briefly:
In double-quoted strings, variables are replaced by their values:
PHP Code:

$word "Hello".
echo 
"$word there!" 

The above code will output:
Hello there!

Doing the same with single quotes:
PHP Code:

$word "Hello".
echo 
'$word there!' 

...will print:
$word there!

Using single quotes where possible, will be slightly more efficient to process, because PHP doesn't have to search single quoted strings for variables.

vharishankar 07-11-2005 10:42 AM

Hmm....

Strings are quite complex, aren't they! I never realized that there were so many parsing options for double quoted strings.

I am glad that I have kept off the complexity and using single quoted strings in my whole application.

lowpro2k3 07-11-2005 10:47 AM

I also use heredocs for large text output (variables are still replaced, this is called interpolation)

Code:


print <<<END_HTML_CODE

  <form method="post" action="$_SERVER['PHP_SELF']">

  <table border="0" width="90%">
    <tr>
      <td><input type="text" name="something" /></td>
    </tr>

    .......

  </table>
  </form>

END_HTML_CODE;


vharishankar 07-11-2005 10:51 AM

@lowpro2k3,

I simply use templating system to output large strings or files ;). I find that it's so much quicker when you build a templating class to parse and output HTML so that the rest of your application can concentrate on the logic.

Try it. Building your own templating system and implementing it can be great fun as I am finding out now. It speeds up application development tremendously and helps keep the logic/code separate from the presentation (output/HTML).

slackie1000 07-11-2005 11:41 AM

hi there,
Quote:

Originally posted by Hko
Briefly:
In double-quoted strings, variables are replaced by their values:
PHP Code:

$word "Hello".
echo 
"$word there!" 

The above code will output:
Hello there!
Doing the same with single quotes:
PHP Code:

$word "Hello".
echo 
'$word there!' 

...will print:
$word there!
Using single quotes where possible, will be slightly more efficient to process, because PHP doesn't have to search single quoted strings for variables.

nice post. well explained...
just adding that this behaviour is pretty similar in script languages when you work with regular expressions...
regards
slackie1000


All times are GMT -5. The time now is 09:16 PM.