Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 06:41 AM
TheKeith
Guest
 
Posts: n/a
Default create_function() and escape\ character question

Hi, I'm just learning php now for the first time and I'm having a little
trouble understanding something.

In the following example:

------------------------------------------------------------------------

<?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?

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);

?>

------------------------------------------------------------------------

Is this behavior something I will encounter often in php, because I
never had this confusion doing javascript or actionscript?


Help would be appreciated.

Keith
  #2  
Old July 17th, 2005, 06:42 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: create_function() and escape\ character question

*** TheKeith wrote/escribió (Mon, 07 Jun 2004 19:41:55 -0400):[color=blue]
> 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?
>
> Furthermore, why does this next single quote version work without the \:[/color]

Sorry, I'm not advanced enough to understand the purpose of
create_function() but I can tell you the difference between single and
double quotes:

* Single quotes are treated as literals:

$foo=33;
echo 'This is $foo'; // prints: This is $foo

* Double quotes are parsed and variables and special chars get replaced by
their values

$foo=33;
echo "This is $foo"; // prints: This is 33

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
  #3  
Old July 17th, 2005, 06:42 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: create_function() and escape\ character question

On Mon, 07 Jun 2004 19:41:55 -0400, TheKeith <no@spam.com> wrote:
[color=blue]
><?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=blue]
>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=blue]
>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
  #4  
Old July 17th, 2005, 06:42 AM
TheKeith
Guest
 
Posts: n/a
Default Re: create_function() and escape\ character question

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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles