473,414 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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
Jul 17 '05 #1
3 2570
*** 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
4
by: Guadala Harry | last post by:
I need to place the following into a string... How can I properly escape the % " / < and > characters? <table width="100%" border="0" cellspacing="0" cellpadding="4px" class="hfAll"></Table> ...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
7
by: Axel Dahmen | last post by:
Hi, within a DataGrid control I'm using a DataTable containing a string column to fill a Hyperlink's href attribute. Unfortunately HttpUtility.UrlEncode() doesn't escape the apostroph character,...
15
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
131
by: Lawrence D'Oliveiro | last post by:
The "escape" function in the "cgi" module escapes characters with special meanings in HTML. The ones that need escaping are '<', '&' and '"'. However, cgi.escape only escapes the quote character if...
8
by: lawrence k | last post by:
Just noticed this for the first time: http://us2.php.net/manual/en/function.create-function.php Anyone know if PHP supports enclosures, like Javascript or Ruby? I've always assumed PHP lacked...
5
by: vlsidesign | last post by:
The printf function returns "warning: unknown escape sequence: \040" for a backslash-space combination. If the ascii decimal number for space is 32 and the backslash is 92, why this particular...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.