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
phpinfo()on both environments and make sure that you are using the same version of PHP and php.ini files. It's possible that you have the extension loaded in one ini file and not the other.