To run a php code directly via cli , there’s a simple command for it.
Lets take a example of traditional php script and how to run it :
<?php echo time();
To run it use via cli:
Now, Lets run it without making the script:
php -r ‘echo time();’
They both would print the same output which is unix timestamp.
More examples of same kind:
php -r ‘echo “Hello World”;’
php -r ‘ print_r( explode( “ “ , ”Hello World” ) );’
php -r ‘var_dump(“Hello World”);’
php -r ‘echo strpos(“Hello World”, “W”);’
php -r ‘$a=10; $b=20; $c=$a+$b; echo $c;’
You can suggest more examples in the comment. Please do to help others.