[nycphp-talk] single quote vs. double quote
Anirudh Zala
arzala at gmail.com
Mon Apr 2 23:42:21 EDT 2007
On Tuesday 03 April 2007 06:46, David Krings wrote:
> Hi!
>
> The old topic is at it again. After some long long time I started again
> with doing some PHP. Task: internationalize my existing project. Goal:
> read strings from a text file. First step: open the file.
>
> I used this:
> $langfile = fopen('$langfileloc', 'r');
> and constantly had it fail. The path and file name are OK, I
> quadruplechecked.
>
> Now, when I do this:
> $langfile = fopen("$langfileloc", "r");
> It works like a charm. Which makes me wonder as some long time ago we
> had this nice discussion that ended with sth like "one needs only the
> single quote for everything in PHP".
>
> Do I recall this incorrectly or are the exceptions (bugs?) in PHP?
>
> Well, took me quite some time to figure out that my use of expert advise
> wasn't that great this time around.
>
> David
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
Please remember below rules while writing PHP expressions.
#1 There is practically NO use of "double quotes" in PHP in writing
expressions EXCEPT expanding sequences like converting "\n" into newline,
"\t" to tabulator (in regex etc.). Which means you should not use "double
quotes" at any other place than above.
#2 When your data is static, use 'single quotes' to tell PHP to use it "as it
is", if dynamic then should not be enclosed by ANY quote.
#3 If you have mixture of static+dynamic then use $dynamic.'I am static' style
to concat dynamic and static data.
When you use "double quotes" PHP will try to EXPAND everything which is
enclosed in "double quotes" which means variables will be expanded, static
string will be looked for constants first and if not found then will be used
as it is and then finally expression will be prepared.
Hope these will clear your thoughts about '' vs. "" quotes.
Anirudh Zala
More information about the talk
mailing list