0

I have a simple variable like this

$arr = [
  'aa' => 'aa',
  'bb' => 'bb'
];

Now, PHP comes with several print out function

print_r

print_r($arr);

result:

Array
(
    [aa] => aa
    [bb] => bb
)

var_dump

var_dump($arr);

result:

array(2) {
  ["aa"]=>
  string(2) "aa"
  ["bb"]=>
  string(2) "bb"
}

I want a function that can print out the original source code like this:

print_out_source_code($arr);

result:

$arr = [
  'aa' => 'aa',
  'bb' => 'bb'
];

Is there any function that can achieve this?

Joseph
  • 3,974
  • 7
  • 34
  • 67
  • 1
    Not built-in as far as I know, but you could write a custom function to simulate this by iterating over the array. You'd likely want to handle multidimensional arrays too, but I think the hardest part would be printing out the name of the variable. https://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php though maybe https://stackoverflow.com/a/404562/1499877 would work for you. – WOUNDEDStevenJones Jun 08 '20 at 04:58
  • I can ignore the variable name, just want the source code. Thank you. – Joseph Jun 08 '20 at 04:59
  • For a more specific answer from anyone you'll need to better define "the source code". Do you care about spaces/formatting? Comments? Do you want _the actual code as written in PHP_ or an emulation of what the source code would be? Does this need to (somehow) work with conditionals and other logic, or just variables (or just arrays)? I suppose you could possibly read a PHP file in PHP and iterate line-by-line until you find the block you want, and then output that? But there are huge security concerns here, so you may want to better explain _why_ you're wanting to do this for a better answer. – WOUNDEDStevenJones Jun 08 '20 at 05:00
  • I can ignore 1. actual source code 2. formating 3. comments I just want valid source code – Joseph Jun 08 '20 at 05:03
  • 1
    Again, I think you need to better define "valid source code". If you have the line `$foo = 'today is ' . date('Y-m-d');` and you call `print_out_source_code($foo);` what should be the output? What about `$bar = $foo; print_out_source_code($bar);`? – WOUNDEDStevenJones Jun 08 '20 at 05:07
  • @WOUNDEDStevenJones I want the evaluated version of valid source code, which is `today is 2020-06-08` – Joseph Jun 08 '20 at 05:10
  • 2
    What I'm getting at is this needs to be better defined... For a string, you could just `echo $foo;` to get that output. For arrays you'd need to manually iterate over them (plus multidimensional functionality). Other variable types you'd need to figure out what you'd want to output (`$foo = 40 + 2;`, etc.). Expanding on what you're actually trying to achieve _and why_ would help get you closer to an answer. – WOUNDEDStevenJones Jun 08 '20 at 05:14
  • Have you checked out `var_export()`? That said, there is no function that can achieve what you are asking for above. The whole formatting info is lost in the process of running the code. As a more general topic, this is called serialization, though it's a relatively special kind of it. BTW: You can (and should!) [edit] your question to clarify it. – Ulrich Eckhardt Jun 08 '20 at 05:33

1 Answers1

1

This should work for the exact "restore array" you're asking.

Try "var_export" and "eval":

$arr = [
  'aa' => 'aa',
  'bb' => 'bb'
];
$filename = 'arr_test.txt';
// save the export, evaluable code of the variable:
$bytes = file_put_contents($filename, var_export($arr, true));
print_r($arr);

// restoring the arr from saved "source code"
$arr = null;
eval('$arr = '.file_get_contents($filename).';');
print_r($arr);
Juanga Covas
  • 315
  • 2
  • 5
  • 1
    If you want to store the contents to a file I think `serialize`/`unserialize` is better than just `var_export`. – Marco Jun 08 '20 at 05:47
  • @marco-a You're right, or just json encode/decode, but I wanted to show by example the eval function as "source code function" he was looking for. – Juanga Covas Jun 08 '20 at 09:01