In PHP, there is a provision for one to successfully execute PHP codes on the command prompt and terminal. To do this, you should do the following. Show
If you don’t have PHP installed, you can install it as shown below. On Windows
On LinuxTo install on Linux, use the command:
On MACYou can run the command below on your terminal to install PHP.
Now that you have PHP installed and running, we can execute our scripts on the command line or terminal. Executing your scripts
The complete code to run our sample script will be as follows.
And with that, you have successfully run your PHP script on the command line or terminal, depending on which you use. Let’s see the image of the execution of my sample
One last thing: you can also start your development server on the cmd and see your code run on the browser. To do that, enter this code in your command line or terminal.
Where You should have something like the image below if you have your browser and cmd opened up. CONTRIBUTOR NDUKWE CHIDERA K.Home / Running PHP scripts as shell scripts As well as running PHP scripts via a web server such as Apache, you can also run PHP scripts from the command line (it’s also possible to write GUI apps with PHP using PHP-GTK, but I’ll maybe
look at that another time). This post looks at the two ways you can run a PHP script from a *nix based computer (Linux, UNIX, BSD, OSX) and the single way you can do it from Windows. UNIX shell scripts are set as executable and the first line in the file contains a “shebang” line which shows the path to the executable which will parse the script. If your distro’s PHP binary is at e.g. /usr/bin/php you would add this to the first line of your script: #!/usr/bin/php and then have your PHP script under it. To run a “hello world script” you’d do this: #!/usr/bin/php <?php echo "hello worldn"; The script then needs to be marked as executable. To set it so it can be run as anyone do this: chmod 0755 /path/to/script.php To make it so only you can run it (and so no one else can even read it) do this: chmod 0700 /path/to/script.php Read the chmod manpage for more details about file permissions if you don’t know about them. I have a copy of the chmod manpage here. Calling the PHP binary, passing the script filenameTo avoid permission issues, or to run the PHP script from the command line on Windows (the above method does not work on Windows unless run via cygwin), you need to execute the PHP binary passing the script filename as the parameter. If you are in the same directory as the script, the script is named e.g. myscript.php, and the PHP binary is at /usr/bin/php you would do this to run it: /usr/bin/php myscript.php If the PHP binary is in your executable path (it usually will be on most Linux distros) then you should just need to run “php” and then the script: php myscript.php If you’re running it from cron it’s always a good idea to put the full path to both: /usr/bin/php /path/to/myscript.php When using this method you don’t need to have the shebang line at the top of the script. An example of doing this on Windows: c:phpphp.exe c:pathtomyscript.php ConclusionIt’s easy to run a PHP script from the command line, which is useful for batch processing or database cleanup for your website. Combined with automatic processing via the cron system and you have a powerful tool at your disposal if you are a PHP programmer and/or your website is programmed in PHP. How do I run a PHP script in Linux terminal?You just follow the steps to run PHP program using command line.. Open terminal or command line window.. Goto the specified folder or directory where php files are present.. Then we can run php code using the following command: php file_name.php.. How do I run a PHP file?php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.
How do I start a PHP script?Run Your First PHP Script. Go to XAMPP server directory. I'm using Windows, so my root server directory is “C:\xampp\htdocs\”.. Create hello.php. Create a file and name it “ hello.php “. Code Inside hello. php. ... . Open New Tab. Run it by opening a new tab in your browser.. Load hello.php. ... . Output. ... . Create a Database. ... . Create a Table.. How do I run PHP in bash?You can execute PHP from the command line in 2 ways:. php yourfile. php.. php -r 'print("Hello world");'. |