By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

create_function() and escape\ character question

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
*** TheKeith wrote/escribió (Mon, 07 Jun 2004 19:41:55 -0400):
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 \:


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
--
Jul 17 '05 #2

P: n/a
On Mon, 07 Jun 2004 19:41:55 -0400, TheKeith <no@spam.com> wrote:
<?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?
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_.
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);
?>
Similar reason - except here, single quotes don't interpolate variables
anyway, so you don't need to escape $.
Is this behavior something I will encounter often in php, because I
never had this confusion doing javascript or actionscript?


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3

P: n/a
Andy Hassall wrote:
On Mon, 07 Jun 2004 19:41:55 -0400, TheKeith <no@spam.com> wrote:

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

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_.

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

Similar reason - except here, single quotes don't interpolate variables
anyway, so you don't need to escape $.

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

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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

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.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.