Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    Function name in string to be evaluated

    I did some research on using eval and call_user_func, and neither works from what I've tried. Code is below:

    PHP Code:
    function qnum()
    {
    $a=1;
    $b=2;
    return 
    $a "#" $b;
    }

    $str="qnum()";
    $q=eval($str);
    echo 
    "q = " $q "<br />"
    I want the echo line to spit out 1#2.

    Any ideas?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The idea of using a function is to pass it some variable(s) and process them.
    A solution, or "answer" is returned.

    Like this:
    PHP Code:
    <?php

    // test variables
    $a=1;
    $b=2;

    $str=qnum($a,$b); 
    echo 
    $str "<br />"

    // increment them
    $a++;
    $b++;

    // and echo them out again ...
    echo qnum($a,$b);

    // The function to take-in any two numbers and spit-out the result  ... example: 1#2
    function qnum($x,$y){ 
    return 
    $x "#" $y

     
    ?>
    This is the result of the script above:
    1#2
    2#3




    .
    Last edited by mlseim; 03-11-2010 at 10:23 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I hate eval so much. Its the only function that I constantly cause a parse error on every time I first write it. Me hates.
    Eval is assigned differently, I can't say I've ever called a function from it off hand, but I believe that it will only return a non-void result if 'return' is actually used within it (not the function it calls). So eval would be:
    PHP Code:
    eval("\$q = qnum();"); 
    Yeah that looks right. I'll bet the first run will fail though O.o hah

    Call_user_func is what you'd want. Eval is as dangerous as it is annoying:
    PHP Code:
    $q call_user_func('qnum'); 
    Much easier. The only time you need to do this is on a <5.3 system with a callback/delegate/function pointer:
    PHP Code:
    function myFunc($data$callback)
    {
        return 
    call_user_func($callback$data);

    Otherwise, there is no reason whatsoever to call a known function with call_user_func.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mathceleb (04-16-2010)

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I guess I totally missed the point of post #1, sorry.

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    I tried the following:

    PHP Code:
    $str="qnum";
    $q1=call_user_func($str);
    echo 
    "q1 = " $q1 "<br />"

    q1 spits out nothing.
    Last edited by mathceleb; 03-11-2010 at 10:50 PM.

  • #6
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Cancel that order, it worked.

    I had one tick mark out of place. Thanks everybody!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •