Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute php block from terminal without saving to a file

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

<?php   Print "Hello, World!"; ?> 

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.

like image 742
Alex Eftimiades Avatar asked Oct 06 '11 02:10

Alex Eftimiades


People also ask

Where should PHP codes be executed?

As a Windows user, though, you'll need to type the full path to the PHP executable to run a PHP script. The PHP executable is usually available at C:\php7\php.exe, so you can use it to execute the PHP file as shown in the following command. For Linux, you don't need to type the whole path to the PHP executable.

How can I run PHP without reloading the page?

PHP is server-side which mean it executes on server. While handling request, first PHP is executed, that the response is returned to user, and then Javacript executes. To communicate between client and server you can use ajax requests, which are basically simple http requests but without reloading whole page.

How do I trigger a PHP file?

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it. Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.


2 Answers

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

php -a 

Details:

enter image description here

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 
like image 157
Mehdi Hosseini Avatar answered Oct 06 '22 00:10

Mehdi Hosseini


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 <path>|<file> 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 <file>        Parse and execute <file>.   -h               This help   -i               PHP information   -l               Syntax check only (lint)   -m               Show compiled in modules   -r <code>        Run PHP <code> without using script tags <?..?>   -B <begin_code>  Run PHP <begin_code> before processing input lines   -R <code>        Run PHP <code> for every input line   -F <file>        Parse and execute <file> for every input line   -E <end_code>    Run PHP <end_code> after processing all input lines   -H               Hide any passed arguments from external tools.   -S <addr>:<port> Run with built-in web server.   -t <docroot>     Specify document root <docroot> for built-in web server.   -s               Output HTML syntax highlighted source.   -v               Version number   -w               Output source with stripped comments and whitespace.   -z <file>        Load Zend extension <file>.    args...          Arguments passed to script. Use -- args when first argument                    starts with - or script is read from stdin    --ini            Show configuration file names    --rf <name>      Show information about function <name>.   --rc <name>      Show information about class <name>.   --re <name>      Show information about extension <name>.   --rz <name>      Show information about Zend extension <name>.   --ri <name>      Show configuration for extension <name>. 
like image 28
alex Avatar answered Oct 06 '22 02:10

alex