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 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2006
    Location
    I'm lost, livin inside myself
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with nested variables

    Okay, here's the problem. I have a string I retrieve from the database that contains several PHP variables, I want to evaluate it and have the variables filled in, I'm just not sure how to do this.

    This is the string I'm retrieving for example:

    Code:
    <b>$test</b>
    before I retrieve it I declare the variable $test, but when I print it out it comes out as this "$test", and doesn't evaluate. How do I do this? I tried using the eval() function and it still doesn't change the output. This is a simplified example of what I'm actually doing to make it easier to solve, if anyone can help I'd appreciate it.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    PHP Code:
    $test "bob";
    $string '<b>$test</b>';

    eval(
    "\$result = ".$string);
    echo 
    $result
    Without seeing your code this is the best I can do - I havent tried it but it should work

  • #3
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This probably isn't as simple as I'm seeing it...but are you wrapping your php in "<?php ?>" ? The example you show is not going to do anything but what you said it does.

    Here's two examples of ways to display the value of $test:
    PHP Code:
    <?php
    $test 
    "blah";
    ?>

    <b><?php print $test?></b>
    OR

    PHP Code:
    <?php
    $test 
    "blah";

    print 
    "<br>".$test."</br>";
    ?>
    You can't mix html and php without telling the browser what is what.

    Hope that helps!

  • #4
    New Coder
    Join Date
    Mar 2006
    Location
    I'm lost, livin inside myself
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's a little more complicated than that, I tried using eval and it doesn't work. Here's a little more complicated version to give you a better idea.

    PHP Code:
    <?php
    $test 
    'test';

    $result mysql_query("SELECT `field` FROM `table` WHERE id='1' LIMIT 1");
    while(
    $row mysql_fetch_assoc($result))
    {
    $string $row['field'//this value retrived is "<b>$test</b>"
    }
    echo 
    $string;

    //output is "<b>$test</b>", not "<b>test</b>" like I want

    ?>
    Again, this is a simplified example of what I'm doing.
    $guiness &= new sixpack();
    $guiness->chug();

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    eval does it, you've just got to make sure the evaled string ends up with double quotes around the target string.

    PHP Code:
    $test 'asdf';
    $othervar 'qwerty';
    $string '<b>$test</b><i>$othervar</i>';

    eval(
    '$replaced_string = "' $string '";');
    echo 
    $replaced_string# <b>asdf</b><i>qwerty</i> 
    A better approach I think would be to use a table:

    PHP Code:
    function make_replacements($string$replacements)
    {
            foreach (
    $replacements as $tag=>$value)
            {
                    
    $string str_replace($tag$value$string);
            }
            return 
    $string;
    }
    $string '<b>$test</b><i>$othervar</i>';
    static 
    $replacements = array( 
            
    '$test'     => 'asdf'
            
    '$othervar' => 'qwerty' 
    );
    echo 
    make_replacements($string$replacements); # <b>asdf</b><i>qwerty</i> 
    Last edited by ralph l mayo; 11-30-2006 at 07:50 PM.


  •  

    Posting Permissions

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