Running php from command line

This is one of those “I know this is possible, but don’t know how”.

php -r '$parts = [ "hello", "world" ]; echo implode( " ", $parts ) . PHP_EOL;' # hello world

Only useful for simple onelines. You’re very likely to be better of putting code in a php file and run that script like:

php ./helloworld.php

Run PHP with WordPress loaded.

Of course your good old friend wp-cli can help.
It can run code with wp fully loaded. So if you add things in the init action or even after the wp_loaded, those plugin/theme functions, posttypes and such are all available.

First off the plain php code execution, with wp eval

wp eval 'var_dump(did_action( "wp_loaded" ));'  # int(1)

Secondly we execute a file with WordPress fully loaded using wp eval-file

wp eval-file ./helloworld.php