Just noticed something, but I'm not sure why this is true, or why it is different than a standard script, maybe someone can help me solve this riddle. When running a PHP script that contains a json_*
function from the command line as an executable I will get a Fatal error: Call to undefined function json_encode() in /var/www/jsontest.php on line 6
with the script below
#!/usr/bin/php -n
<?php
$arr = array('foo', 'bar', 'baz');
print_r($json = json_encode($arr));
print_r(json_decode($bar));
This also happens with json_decode() when trying to decode standard clean json input (validated via jsonlint)
The above script was being run as follows, in a Linux/DEB terminal...
$ chmod +x jsontest.php
$ ./jsontest.php
By running this through my local webserver though, I receive my expect output
#!/usr/bin/php -n ["foo","bar","baz"]Array ( [0] => foo [1] => bar [2] => baz )
What is going on? Why is JSON not available when interpreted as an executable?
PHP version is PHP 5.5.3-1ubuntu2 (cli) (built: Oct 9 2013 14:49:24)
and PHPInfo for anyone who might want it is published on my Ubuntu One Account