xyz
xyz

Reputation: 8917

How to run a php code using json_decode?

I am learning to use json_decode. I want to try out some php code snippet using this, but I don't want to setup whole web service kind of system using web server,html etc etc..

Can I run some standalone php code on linux and see its output?

Upvotes: 1

Views: 885

Answers (3)

Igor Malyk
Igor Malyk

Reputation: 2656

If you don't have PHP installed at your system at all you might want to install it and the CLI as well. For example on Ubuntu like systems it is done by:
sudo apt-get install php5-common php5-cli
For other systems see PHP Installation on Unix systems
Then you can make a little PHP file:

    <?php
        echo "Hello, World !!!" . PHP_EOL;

and save it as hello.php then run it by

        php -f hello.php

For the little code snippets like that you might want try this

        php -r 'echo "Hello, World !!!" . PHP_EOL' 

this should output the same result;

It can be used instaed of a calculator for tasks like:

    php -r 'echo pow(2,3) * 2 . PHP_EOL;'

which outputs 16

as of json, try with

    echo json_encode(array("foo" => "bar"));

which gives

    {"fff":"dfsf"}

and

    print_r( json_decode('{"fff":"dfsf"}') );

which outputs

    stdClass Object
    (
        [fff] => dfsf
    )

Upvotes: 1

Sebastian Thiele
Sebastian Thiele

Reputation: 160

Yes you can. Try a example from php.net. Save it as json.php (or so) and run it on the console. (if you have installed php (which php will help))

go to the folder you saved the file and run:

php json.php

that's it. The output will shown in the stdout (the console).

Upvotes: 1

Quentin
Quentin

Reputation: 943639

PHP has a command line interface

php myScript.php

(Note that many Linux distributions have separate packages for mod_php and php cli, so you might need to install it before you can use it)

Upvotes: 5

Related Questions