+

How do I execute PHP code using the command line?

Install PHP for Windows Users:Follow the instructions for installing PHP on Windows.
  • Step 1:First, we have to download PHP from the official site . We have to download the .zip file from the appropriate section depending on the architecture of our system (x86 or x64).
  • Step 2:Extract the .zip file to the desired location. It is recommended to select the boot drive (C drive) inside a folder named php (i.e. C: / php).
  • Step 3:Now we need to add a folder (C: / php) to the path of the environment variable to make it available from the command line. To do this, right-click the My Computer or This Computer icon, and then select Properties from the context menu. Then click on the Advanced System Settings link and select Environment Variables. In the System Variables section, we have to find the PATH environment variable and then select and edit it. If the PATH environment variable does not exist, we must click New. In the Edit system variable (or new system variable) window, we have to specify the value of the PATH environment variable (C: / php or the location of our extracted php files). After that we have to click OK and close all the remaining windows by clicking OK.
  • Install PHP for Linux users:
    • Linux users can install php using the following command.
      apt-get install php5-common libapache2-mod -php5 php5-cli 
      It will install PHP with Apache server. For more information click here .
      • Install PHP for Mac users:
        • Mac users can install php using the following command.
          curl -s https://php-osx.liip. ch / install.sh | bash -s 7.3 
          It will install php on your system. For more information click here .
          • After installing PHP, we are ready to run the PHP code via the command line. You just follow the instructions to run the PHP program using the command line.
            • Open a terminal or command prompt window.
            • Change to the specified folder or directory where the php files are present.
            • Then we can run the php code with the following command:
              php file_name.php 
            • We can also start the server to test php code using the command line with the following command:
              php -S localhost: port -t your_folder / 
            Note.When using the built-in PHP server the PHP file name inside the root folder should be index.php, and all other PHP files can be hyperlinked through the main index page.
Get Solution for free from DataCamp guru