Can i run php in terminal?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP Installation for Windows Users: Follow the steps to install PHP on the Windows operating system.

    • Step 1: First, we have to download PHP from it’s official website. We have to download the .zip file from the respective section depending upon on our system architecture[x86 or x64].
    • Step 2: Extract the .zip file to your preferred location. It is recommended to choose the Boot Drive[C Drive] inside a folder named php [ie. C:\php].
    • Step 3: Now we have to add the folder [C:\php] to the Environment Variable Path so that it becomes accessible from the command line. To do so, we have to right click on My Computer or This PC icon, then Choose Properties from the context menu. Then click the Advanced system settings link, and then click Environment Variables. In the section System Variables, we have to find the PATH environment variable and then select and Edit it. If the PATH environment variable does not exist, we have to 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 remaining windows by clicking OK.

    PHP Installation 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.

      • PHP Installation for Mac Users:

        • Mac users can install php using the following command.
          curl -s //php-osx.liip.ch/install.sh | bash -s 7.3

          It will install php in your system. For more information click here.

          • After installation of PHP, we are ready to run PHP code through command line. 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

            • We can also start server for testing the php code using the command line by the following command:
              php -S localhost:port -t your_folder/

            Note: While using the PHP built-in server, the name of the PHP file inside the root folder must be index.php, and all other PHP files can be hyperlinked through the main index page.

            PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

    Say I have a block of code I would like to test like this:

    
    

    How I quickly run this code from terminal without saving it to a file?

    I tried things like...

    php -r "Print "Hello, World!";"
    

    but just got complaints about syntax errors. There has to be a simple way of doing this. I just have yet to find any explanations.

    alex

    466k197 gold badges864 silver badges975 bronze badges

    asked Oct 6, 2011 at 2:56

    Alex EftimiadesAlex Eftimiades

    2,4473 gold badges24 silver badges33 bronze badges

    0

    To run PHP commands immediately on Terminal you can pass -a option to your installed PHP:

    php -a
    

    Details:

    php -a opens an interactive shell which let you type directly PHP commands and view the result on your terminal, As an example, after typing php -a in terminal you can type echo 'Hello World'; and after Press Enter Hello World! will be printed on the screen.

    Windows Solution

    On windows there is no interactive mode same as Linux but still you can use interactive like mode!, So, open PHP on place you installed it for example if you use XAMPP then your PHP should be is on C:\xampp\php [or add the binary directory to environment variables] and then type php -a, At the end of each line you can view the results by pressing Ctrl+Z and then Enter.

    php -a
    echo 'hello world!';
    ^Z
    

    answered Nov 9, 2012 at 0:28

    Mehdi HosseiniMehdi Hosseini

    1,6671 gold badge17 silver badges29 bronze badges

    3

    Escape the inside double quotes ["] that you are using to delimit your string.

    php -r "Print \"Hello, World!\";"
    

    Alternatively, use single quotes ['] for the PHP string or for the quoting of the PHP code.

    If you run php --help you can see a list of commands that the php program accepts.

      -a               Run as interactive shell
      -c | Look for php.ini file in this directory
      -n               No php.ini file will be used
      -d foo[=bar]     Define INI entry foo with value 'bar'
      -e               Generate extended information for debugger/profiler
      -f         Parse and execute .
      -h               This help
      -i               PHP information
      -l               Syntax check only [lint]
      -m               Show compiled in modules
      -r         Run PHP  without using script tags 
      -B   Run PHP  before processing input lines
      -R         Run PHP  for every input line
      -F         Parse and execute  for every input line
      -E     Run PHP  after processing all input lines
      -H               Hide any passed arguments from external tools.
      -S : Run with built-in web server.
      -t      Specify document root  for built-in web server.
      -s               Output HTML syntax highlighted source.
      -v               Version number
      -w               Output source with stripped comments and whitespace.
      -z         Load Zend extension .
    
      args...          Arguments passed to script. Use -- args when first argument
                       starts with - or script is read from stdin
    
      --ini            Show configuration file names
    
      --rf       Show information about function .
      --rc       Show information about class .
      --re       Show information about extension .
      --rz       Show information about Zend extension .
      --ri       Show configuration for extension .
    

    answered Oct 6, 2011 at 2:58

    0

    Can we run PHP in terminal?

    After installation of PHP, we are ready to run PHP code through command line. 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.

    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 run a PHP file on Mac?

    Use the Built-In Apache Web Server to Run PHP on Mac We can use the command sudo apachectl start in the terminal to start the webserver. Then, typing the URL //localhost/index.php where our PHP file is index. html will run the PHP file. The PHP file should be in the root directory to run.

    How do I start PHP code?

    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..

    Chủ Đề