borisrepl / boris

A tiny REPL for PHP
MIT License
2.15k stars 115 forks source link

Can not echo global variable #120

Open ghost opened 8 years ago

ghost commented 8 years ago

Hello This is my code.

phper@phper:~$ boris
[1] boris> $books = ['aaa', 'bbb'];
// array(
//   0 => 'aaa',
//   1 => 'bbb'
// )
[2] boris> function serveBooks() {
[2]     *> global $books;
[2]     *> var_dump($books);
[2]     *> }
[3] boris> serveBooks();
NULL
// NULL
[4] boris>
dhotson commented 8 years ago

You need to specify that the outer $books variable is global. e.g.

[1] boris> global $books;
[2] boris> $books = ['aaa', 'bbb'];
// array(
//   0 => 'aaa',
//   1 => 'bbb'
// )
[3] boris> function serveBooks() {
[3]     *> global $books;
[3]     *> var_dump($books);
[3]     *> }
[4] boris> serveBooks();
array(2) {
  [0]=>
  string(3) "aaa"
  [1]=>
  string(3) "bbb"
}
// NULL