2

i want to add/create a php file with inside an array using file_put_contents().

$arr= array('red');
$content= "<?php \n$".$var."=".$arr."\n?>"; 
file_put_contents('file.php', $content);    

But then if i open file.php i read this:

 <?php
 $var=Array
 ?>

i would to see array('red')

I tried also serialize() but the output is bad...

1 Answer 1

1

Converting an array to a string just returns Array. You can use var_export() to get an array literal to use there.

$content = "<?php \n\$" . $var . "=" . var_export($arr, true) . "\n?>";
Sign up to request clarification or add additional context in comments.

Comments

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.