Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How transform a string into a variable name – PHP?

I have a php file ("gion.php") with inside an array:

 $gion = array('ok');

I want to include this file and print the array.

 $name_user = "gion";
 include "profili/".$name_user.".php";
 $file_profile= "$".$name_user;

 print_r($file_profile);

print_r doesn’t work because i suspect $file_profile is a string, so i can change it in a name variable ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"$gion" (string) -> $gion (variable) 

thanks a lot

EDIT: i want to print array(‘ok’) and not "gion"

>Solution :

File: gion.php:

$gion = array('ok');

File: main.php (or whatever your filename is):

$name_user = "gion";
include "profili/" . $name_user . ".php";
$file_profile = ${$name_user};

print_r($file_profile);   // this also works: print_r($gion);

This will print:

Array
(
    [0] => ok
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading