Execute eval in eval

Discussion in 'PHP' started by dan101, Apr 9, 2010.

  1. #1
    I have something like that

    $first="echo 'Hello';";
    
    $intro 'eval ($first)';
    
    eval("\$intro = \"$intro\";"); 
    Code (markup):
    The output is
     eval ( echo 'Hello'; ); 
    Code (markup):
    But I want to display only Hello .. how I can fix this- to execute the first eval then the second.
     
    dan101, Apr 9, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    To answer your question:

    <?php
    $first="echo 'Hello';";
    $intro='eval ($first);';
    eval($intro);
    ?>
    
    PHP:
    Output: Hello

    However. You would be well advised to do this another way - read up on eval if unsure why and remember;

    " If eval() is the answer, you're almost certainly asking the wrong question " (Rasmus Lerdorf, creator of PHP)

    Without knowing what you are trying to do;

    <?php
    $first = "Hello";
    $intro = "first";
    print $$intro;
    ?>
    PHP:
    Output: Hello
     
    Last edited: Apr 9, 2010
    lukeg32, Apr 9, 2010 IP
  3. dan101

    dan101 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you .. works perfect. I want to use it for a template file that contains encoded php code. So I want to use eval to execute the decoded php code

    If eval is not good could you recommend another solution?

    Thank you again.
     
    dan101, Apr 9, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    No problem at all.

    I don't understand why people bother encrypting data in free themes; It serves as a hinderance to those that are not familiar with editting the file, but in most cases these people done understand HTML either - or wordpress / themes for that matter, hence why they use a free theme.

    Its also trivial to decode it anyway in most cases, is it really worth your time?

    If the requirement is to leave the link then so be it; most honest people will adhere to this but theres always going to be scumbags that ignore it, its the same with anything really, articles, images etc.... given the type of sites these people build, they are probably doing you a favour anyway, you dont really want them linking to you :)

    ( Personally, If i don't have the time to create my own, i always leave the link in place as a "thanks" to the author for saving me a few hours .... or if i really like it I end up buying the version that lets me remove it...... )
     
    lukeg32, Apr 9, 2010 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    I disagree. I think eval() get's a bad wrap because of people's inability to use it safely. If used carefully, it can be quite useful.
     
    Alex Roxon, Apr 9, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Oh dear. I think it gets a bad wrap because of peoples inability to code properly and so use eval()
     
    lukeg32, Apr 10, 2010 IP
  7. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I aggre Luke, eval gets a bad wrap because it's only common use is to be useless or a security hole.

    The only good use of it I have seen is in Drupal's debugging module Devel.
     
    Warll, Apr 10, 2010 IP