Jens Törnell
Jens Törnell

Reputation: 24768

PHP get string by string

Let's say I have the following code

json_decode( $images_json );
json_decode( $cites_json );

But instead I want to use the input by a dynamic string, like this...

json_decode( {{$name}}_json );

...where $name is a string containing "images" or "cites".

Is it possible? How?

Upvotes: 0

Views: 37

Answers (2)

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

Use this;

json_decode( ${$name . '_json'} );

You can refer here for further detail

Upvotes: 2

xdazz
xdazz

Reputation: 160853

You could do:

json_decode( ${$name.'_json'} );

But variable variables is not good idea usually, you may use an array to refactor your code.

Upvotes: 2

Related Questions