Execute a string of PHP code on the command line

To execute a string of PHP code on the command line, you can use the "php" command followed by the "-r" option and the string of code in quotes. For example:

<?php 
    php -r 'echo "Hello, world!";'
?>

This will execute the code "echo 'Hello, world!';" and print "Hello, world!" to the console.

Watch a course Learn object oriented PHP

You can also save your PHP code in a file with the .php extension and then run the file via the command line with the php command followed by the file name, for example:

php myfile.php

This will execute the code in myfile.php