Andy Hassall wrote:
[color=blue]
> On Mon, 07 Jun 2004 19:41:55 -0400, TheKeith <no@spam.com> wrote:
>
>[color=green]
>><?php
>>function test ($thenum) {
>> $funct = create_function("\$thenumber", "return (\$thenumber+5);");
>> echo $funct($thenum);
>>}
>>test(5);
>>?>
>>
>>------------------------------------------------------------------------
>>
>>I don't understand why the escape characters are necessary here. I
>>understand that the create_function() function requires its parameters
>>to be in string format, but if my $thenum variable is an integer and not
>>a string, why is the \ necessary?[/color]
>
>
> Because otherwise $thenumber inside the function definition gets interpolated
> to a value _before_ it reaches create_function.
>
> Consider without the escapes:
>
> function test ($thenum) {
> $funct = create_function("$thenumber", "return ($thenumber+5);");
> echo $funct($thenum);
> }
>
> Forget that this is a create_function for the moment, and just look at the
> strings.
>
> "$thenumber"
>
> is the same as
>
> $thenumber
>
> i.e. undefined, as there's no variable of that name in scope at the moment.
>
> "return ($thenumber+5);"
>
> is similarly
>
> "return (+5);"
>
> ... because there's no $thenumber in this scope.
>
> But with the escapes in, the text gets passed to create_function as is, and so
> $thenumber is a variable _within the scope of the newly created function_.
>
>[color=green]
>>Furthermore, why does this next single quote version work without the \:
>>
>>------------------------------------------------------------------------
>>
>><?php
>>function test ($thenum) {
>> $funct = create_function('$thenumber', 'return ($thenumber+5);');
>> echo $funct($thenum);
>>}
>>test(5);
>>?>[/color]
>
>
> Similar reason - except here, single quotes don't interpolate variables
> anyway, so you don't need to escape $.
>
>[color=green]
>>Is this behavior something I will encounter often in php, because I
>>never had this confusion doing javascript or actionscript?[/color]
>
>
> Double quotes (which interpolate any variables inside) versus single quotes
> (which don't) are all over the place in PHP. The confusion here is more due to
> the interaction with create_function - where you don't want the variables
> interpolated now, you want it to happen when the anonymous function executes.
>
> --
> Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
>
http://www.andyh.co.uk /
http://www.andyhsoftware.co.uk/space[/color]
Ahh thanks, I think I understand now--so if it were put with the double
quotes but without the escape, it would be the same as:
------------------------------------------------------------
<?php
function test ($thenum) {
$funct = create_function("", "return (""+5);");
echo $funct($thenum);
}
test(5);
?>
-------------------------------------------------------------
since $thenumber has no value associated with it at the time of the
create_function() function's initial execution, correct? In this case,
my confusion was due to the fact that I assumed the create_function()
function worked like a regular function in not reading its contents
until specifically called, but that's not the way it works at all, right?
Thanks again.