eval problem

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

    eval problem

    Hi,
    I have to eval a string stored in a variable, but it's not working.
    code:
    ___

    function ee($s)
    {
    return $s;
    }

    $v="e(\"name\") ";

    eval("$stri=$v; ");
    echo $stri;
    ___

    By using eval I want to eval $v and get the result returned by ee
    stored in $stri. How woul you do?

  • Janwillem Borleffs

    #2
    Re: eval problem

    iulian.ilea wrote:
    By using eval I want to eval $v and get the result returned by ee
    stored in $stri. How woul you do?
    >
    Why use eval at all?

    function ee($s) { return $s; }
    $str = ee("Bob");
    print $str;


    JW


    Comment

    • iulian.ilea

      #3
      Re: eval problem


      Janwillem Borleffs wrote:
      iulian.ilea wrote:
      By using eval I want to eval $v and get the result returned by ee
      stored in $stri. How woul you do?
      >
      Why use eval at all?
      >
      function ee($s) { return $s; }
      $str = ee("Bob");
      print $str;
      >
      >
      JW
      Thanks, but what I wrote above is just an example. I take the variable
      that must be eval'd from an sql table.

      Comment

      • Oli Filth

        #4
        Re: eval problem

        iulian.ilea said the following on 24/09/2006 12:54:
        Janwillem Borleffs wrote:
        >iulian.ilea wrote:
        >>function ee($s)
        >> {
        >> return $s;
        >> }
        >>>
        >>$v="e(\"name\ ")";
        >>>
        >>eval("$stri=$ v;");
        >>echo $stri;
        >>By using eval I want to eval $v and get the result returned by ee
        >>stored in $stri. How woul you do?
        >>>
        >Why use eval at all?
        >>
        >function ee($s) { return $s; }
        >$str = ee("Bob");
        >print $str;
        >
        Thanks, but what I wrote above is just an example. I take the variable
        that must be eval'd from an sql table.
        This leads to a further question: Why does your database contain PHP
        code? This goes against normal practices of both database and PHP
        design. You may be aware of the popular quotation:

        "If eval() is the answer, you're almost certainly asking the wrong
        question."

        i.e. there's probably a better, safer, more programmatic way of doing
        what you're doing.


        However, the answer to your original problem is that if you want the
        string literal '$stri', you must escape the $ character, i.e.:

        eval("\$stri = $v;");

        Otherwise the value of $stri is substituted into the expression to be
        evaluated (and presumably at this point $stri doesn't exist).


        --
        Oli

        Comment

        • Colin McKinnon

          #5
          Re: eval problem

          iulian.ilea wrote:
          >
          By using eval I want to eval $v and get the result returned by ee
          stored in $stri. How woul you do?
          Exactly the way its written in the manual (http://uk2.php.net/eval)

          $v = 'e("name")';
          $v="\$stri = " . $v . ";";
          eval($v);
          print $stri;

          Eval (and storing code in a database) is very dangerous unless you know what
          you're doing - and anyone who know what they were doing would RTFM first.
          You have not done that therefore we must assume you don't know what you are
          doing.

          And if you did know what you were doing you would be trying very hard to
          avoid the scenario where this is the solution.

          C.

          Comment

          • Tim Roberts

            #6
            Re: eval problem

            "iulian.ile a" <iulian.ilea@gm ail.comwrote:
            >
            >I have to eval a string stored in a variable, but it's not working.
            >code:
            >___
            >
            >function ee($s)
            {
            return $s;
            }
            >
            >$v="e(\"name\" )";
            >
            >eval("$stri=$v ;");
            >echo $stri;
            >___
            >
            >By using eval I want to eval $v and get the result returned by ee
            >stored in $stri. How woul you do?
            Well, for one thing, I would not try to call a function called "e" when I
            had named the function "ee".
            --
            - Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            Working...