-1

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.

1
  • nevermind i'm dumb. i had to return the $code. Commented Mar 6, 2015 at 14:45

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

-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.

2 Comments

"option 1 needs a semicolon as final sign at the end of the php command" - what does that mean? I haven't found any occurence of $text in the given question
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.