Ach.. a sad sorry programming question

Discussion in 'OT Technology' started by glass, Jan 11, 2006.

  1. glass

    glass New Member

    Joined:
    Apr 12, 2004
    Messages:
    400
    Likes Received:
    0
    it's pretty simple..

    Question:
    how do you execute instructions stored as a string?

    say i have a string called $x, where

    $x = "doSomething()";

    where doSomething() is a function that does something. it's stored as a string because it was returned from another file.

    i'm kinda embarassed to be asking this because i've been programming for more than a year now, and i have no idea how to do something this simple.

    here's the full problem if anyone's interested:
    ====================================
    it's a PHP problem. i'm looking for a function that will do the following;

    say i have a text document "test.txt" that has just the following saved in it:
    Code:
    if ($here) {
    print '<b>hello</b>';
    }
    else {
    print '<b>goodbye</b>';
    }
    
    and a php file that has the following code:

    Code:
    < ?php
    $here = true;
    $cont = file_get_contents('test.tst');
    X;
    ? >
    
    where X is some function that returns the contents of test.txt into the php code. in this case, the php file should display "hello".

    basically what i want to do is take all my if/else statements, stick them into a text file, and just load them from that file from any number of pages. how would i go about doing this?
     
  2. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
  3. glass

    glass New Member

    Joined:
    Apr 12, 2004
    Messages:
    400
    Likes Received:
    0
    thanks for that, but actually i had some trouble figuring out how that worked. i found out eval() did the trick nicely though, so i might be using that (and i'd recommend it if you're just trying to execute the contents of a string). i'm sure i'll find uses for create_function though now that i'm aware of it.
     

Share This Page