I have a .html file which contains the following:
array('1', '786286', '45626');
That is literally all it contains.
In my code, I want to eval this and then print it:
$code = file_get_contents('array.html');
$nums = eval($code);
print_r($nums);
However, it is not printing anything.
Any help would be greatly appreciated.
First off, don't use eval()
. it's an EVIL function and should be avoided like the plague.
Secondly, it's not working, because you haven't written proper PHP code. What your eval is doing is the literal equivalent of having a .php file which contains:
<?php
array(1,2,3)
There's no assignment, there's no output, and there's no return
. There's simply an array being "executed" and then immediately being destroyed.
What you should have is
<?php
$arr = array(1,2,3)
so the array gets preserved. Which means your eval should look more like:
$text = file_get_contents('...');
// option 1
eval("\$arr = $text");
^^^^^^^
print_r($arr);
// option 2
$foo = eval("return $text");
^^^^^^
print_r($foo);