create_function() and escape\ character question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TheKeith

    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 ("\$thenumbe r", "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
  • Alvaro G Vicario

    #2
    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
    --

    Comment

    • Andy Hassall

      #3
      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 ("\$thenumbe r", "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

      Comment

      • TheKeith

        #4
        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 ("\$thenumbe r", "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.

        Comment

        Working...