Reputation: 1509
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.
Upvotes: -1
Views: 1848
Reputation: 1
option 1 needs a semicolon as final sign at the end of the php command, means:
eval("\arr = $text;");
I suspect, option 2 analogously needs this semicolon, too.
Upvotes: -1
Reputation: 360672
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);
Upvotes: 3