NYCPHP Meetup

NYPHP.org

[nycphp-talk] single quote vs. double quote

Paul Houle paul at devonianfarm.com
Wed Apr 4 18:41:12 EDT 2007


David Krings wrote:
> Paul Houle wrote:
>>    (4) Make a habit of writing {$like_this}
>
> Can you elaborate on this? Me guessing of what you mean is probably 
> not a good approach. Thanks in advance.
>
    There's a short form and long form of substitution in PHP.  The 
short form is

$x="$y an example of the short form";

and

$x="{$y} is an example of the short form";

    You can get in trouble with the short form because it's greedy.  
Imagine you're trying to make the name of a logfile

$logfile_name="$year_$month_$day_logfile.txt";

    PHP evaluates "$" expressions in a greedy manner,  so it will look 
up the variables

$year_
$month_
$day_logfile

rather than

$year
$month
$day

that you probably want.

$logfile_name="{$year}_{$month}_{$day}_logfile.txt";

gets the desired effect.   The long form also lets you do cool things 
with arrays and object,  like

"{$my_array[$index]}"
"{$my_array["i_can_really_use_quotes_to_have_a_string_here"]}"
"{$object->special_property}"






More information about the talk mailing list