简体   繁体   中英

PHP - Function echo not replace variable from a JSON object-string

I would like to print the value of the variable '$fruit' directly with the php function echo.

If i use a string this work properly, but if the string is previous retrieve by a JSON object php not sostitute the variable with his value.

The above php code illustrate clearly the issue.

<?php

$json = '{

        "object_1":
            {
                "0": "banana",
                "1": "$fruit"

            }
            
        }';
            
$fruit = "mango";

$object_json = json_decode($json, false);

$var = $object_json->object_1->{1} ;

echo "$fruit "; // result: mango

echo $var; // result: $fruit, expected: mango

?>

Thank a lot four your help!

You can use the $$variableName notation to refer to a local variable by a dynamic variable name.

<?php
    $json = '{
        "object_1": {
            "0": "banana",
            "1": "$fruit"
        }
    }';

    $fruit = "mango";

    $object_json = json_decode($json, false);

    $var = $object_json->object_1->{1};

    var_dump($var); //string(6) "$fruit"
    var_dump($fruit); //string(5) "mango"

    if (strlen($var) >= 1 && $var[0] == '$') {
        $variableName = substr($var, 1); //string(5) "fruit"
        $var = $$variableName; //expanded $fruit
    }

    var_dump($var); //string(5) "mango"
?>

DEMO

Note :

Injecting variables like this can be very dangerous. Make sure that your JSON is from a trusted source, and not a client/browser. This has the same vulnerabilities as extract() does - so check out the warnings in the manual.

           <?php
                $json = '{
              "object_1": {
              "0": "banana",
               "1": "$fruit"
                   }
               }';

   $fruit = "mango";

    $object_json = json_decode($json, false);

      echo $newFruit=$object_json->object_1->{0};
          echo $oldFruit=$object_json->object_1->{1};

?>

It is because the double quote means template string in php, and the single quote means static string.

echo '$fruit '; // result: $fruit
echo "$fruit "; // result: mango

You have some conceptual failures within the code.

Check out this version of the same code that do what you expect:

<?php
$fruit = "mango";
$json = "{
        'object_1':
            {
                '0': 'banana',
                '1': '$fruit'

            }

        }";


$object_json = json_decode($json, false);

$var = $object_json->object_1->{1} ;

echo "$fruit"; // result: mango

echo $var; // result: mango
?>

One error on your code is that you reference a variable before that you defined it. The second one it's that you can't expect variable replacement with single-quoted strings. This happen with double-quoted strings.

Nevertheless, perhaps what do you want is to replace the variable as soon as you have the value of it (perhaps you get the value from a webservice, for example). In that case you will need to use some template library, or do it yourself with some regular expressions. With this method, you can do it in the way that you were trying to do it.

I hope that this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM