Hi good people.

I need some help with this small piece of code below:

$mode = “+”;
$processNumbers = create_function( ‘$a, $b’, “return \$a $mode \$b;” );
echo $processNumbers( 2, 3 );

The outcome of this code displays '5'.

But the thing i don't understand is, why there are backslashes before the variables:

\$a and \$b ??????

why not just $a and $b ?????

Paul.

    See the description for [man]create_function[/man] in the manual. It's the same reason you'd escape dollar signs in other double-quoted strings.

      hey. can i just ask.. when php people refer to 'haystack' and 'needle',,, do they mean the whole code (haystack) and a snippet of that code (needle)??

        Some context might help, but it's almost assuredly a reference to the idiom/cliché "a needle in a haystack." If in the context of functions like [man]strstr/man, "haystack" is the collection of data to be searched, "needle" is thing for which you're searching.

          could you give me an example, indicating the haystack and the needle, if possible?

            also.... back to my original question. when you say 'escape dollar signs',, does this mean to make it so you can print or echo the literal variable name as a literal sting output??

            eg:

            $variable = 5;

            echo $variable

            output:

            $variable,,,,,,,,,, not '5'

              Paul help!;11033077 wrote:

              could you give me an example, indicating the haystack and the needle, if possible?

              $haystack = "Hello, world!";
              $needle = ",";
              
              if(strpos($haystack, $needle) !== FALSE)
                  echo "\$haystack contains \$needle!";
              else
                  echo "\$haystack doesn't contain \$needle!";
              Paul help!;11033079 wrote:

              also.... back to my original question. when you say 'escape dollar signs',, does this mean to make it so you can print or echo the literal variable name as a literal sting output??

              Sort of the other way around; you can include what looks like an actual variable name inside of a string. See my above (dual-purpose) example code; the echo messages will contain a literal dollar sign followed by the word 'needle', not a comma (i.e. the contents of the variable $needle).

                11 days later

                ok. this is what i don't understand...............

                in the above example you just gave me: \$haystack and \$needle variables are escaped... like you said, they will only echo as \$haystack and \$needle and not there containing value.

                But in this example:

                $mode = “+”;
                $processNumbers = create_function( ‘$a, $b’, “return \$a $mode \$b;” );
                echo $processNumbers( 2, 3 );

                ........... “return \$a $mode \$b;” .......... \$a and \$b are escaped but still seem to contain there values.

                in the book i am reading, it does not give a reason for this.

                how on earth can $mode add to escaped variables together?

                  In that code, the string contains literal dollar signs followed by an "a" and a "b". This is because [man]create_function/man takes the contents of that string and executes it as if it were PHP code.

                  EDIT:

                  how on earth can $mode add to escaped variables together?

                  It doesn't; the code being executed is:

                  return $a + $b;

                  If you didn't escape $a and $b inside the double quote delimited string, you'd get undefined variable errors since $a and $b don't exist at that point.

                    ok. so if it were in single quotes, there would be no need to escape the variables?

                      Right... except you'd have to use concatenation since $mode is meant to be interpolated rather than appear literally in the string.

                        could you show me what you mean by that by showing me the same code but in single quotes?

                          This:

                          "return \$a $mode \$b;"

                          is equivalent to this:

                          'return $a ' . $mode . ' $b;';
                            Write a Reply...