简体   繁体   中英

Declaring PHP Variables from a .txt file

I have a text file with the following data

//info.txt
$ip = "192.168.12.201"
$sno = "some text"
$mac = "some text"

I want to be able to convert these variables from the text file into PHP variables to be checked with presaved data. I'm able to do this in python but it causes and extra redundant step. Could what I'm trying to achieve be done in PHP?

For this approach, you need to read the file first, then use eval() to execute the command inside the txt file.

<?php   
$text = file_get_contents("info.txt");
eval($text);
// display variable
var_dump($ip);
var_dump($sno);
var_dump($mac);

But, I'm not sure this approach is secure

Variable variable is not recommended, but you can do this: readline to get value like: $value1 = 'ip': $value2 = '192.168.12.201' so:

$$value1 = $value2;

thats mean

$ip = '192.168.12.201';
$var = 123;

$val = "testo";

${"_testoacaso_".$var} = $val;

$_testoacaso_123 = $val;

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