How to use and run PHP scripts from the Linux command line

Source: Internet
Author: User
Tags php cli php debugging tools php script phpinfo

Check PHP information

# php-f/var/www/html/infophp.php

Checking PHP information from the command line

Because the output is too large, we can pass the above output to the command through the pipeline, less so we can output one screen at a time, the command is as follows:

# php-f/var/www/html/infophp.php |

Check all PHP information

Here, the '-f ' option resolves and executes the file followed by the command.

2. We can use this very valuable debugging tool directly on the Linux command line without phpinfo() having to call it from a file, just execute the following command:

# php-r ' phpinfo (); '

PHP Debugging Tools

Here, the '-r ' option will allow the PHP code to execute directly in the Linux terminal without < and > tagging.

usage php [-Q] [-h] [-S] [-V] [-I.] [-F] | {[Args ... ]}-q Quiet mode, do not output HTTP header -s to convert PHP program files into color format HTML (such as reserved words in green, functions and variables are blue, comments are yellow and strings are red and so on -F Read into and interpret the specified file- c in the read PHP. INI file -a interactive run -D foo[=Bar] Defines the input entries in INI foo for bar-e output additional information for debugging and performance analysis -Z Transfer into Zend Extension file -i PHP related information -H Help

Specific usage examples:

/usr/local/bin/php/home/script/test.php

Directly execute the/home/script/test.php file

nohup/usr/local/bin/php/home/script/test.php >/home/script/result.log

Execute the/home/script/test.php file without hanging up (background) and redirect the output to the/home/script/result.log file

*/1 * * * * root-q/usr/local/bin/php/home/script/test.php

/home/script/test.php This script every other minute

Note:

(1) If you do not know the PHP executable directory, you can directly enter the command where is PHP to get the Php.exe directory

(2) Before executing the command, you need to turn the executed file into an executable script file. Example: chmod +x test.php

Note:

1. Use the Php-q file name. php to use the PHP program as a shell program,

2. Use the-S to HTML your PHP program. Is this very worry?

3. Use PHP's ODBC functionality to manipulate the database in shell commands.

How to run a PHP script [with parameters] (Linux) at the command line

Keyword: run php under command line

#php-V shows the version of PHP

Create a simple text file that contains the following PHP code and save it as hello.php:
<?php
echo "Hello from the CLI";
?>
Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:
#php phphello.php
Output Hello from the CLI

----------------- use of standard inputs and outputs
You can use these three constants in your PHP scripts to accept user input, or to display the results of processing and calculations. To better understand this, take a look at the following script (

List A):
 <? php  //  ask for input  fwrite  (STDOUT, "Enter Your Name:"  //  get input   $name  = trim  ( fgets   //  write input back  fwrite  (STDOUT, "Hello,  $name !" ? >look What happens if you run it : shell  > PHP Hello. phpenter your name :  Joehello , joe! 

In this script, the fwrite () function first writes a message to the standard output device asking for the user's name. It then reads the user input information obtained from the standard input device

Takes a PHP variable and merges it into a string. The string is then printed out to the standard output device using fwrite ().

----------------- using command-line arguments
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program. The PHP CLI has two special variables specifically designed to achieve this

Purpose: One is the $ARGV variable, which saves the arguments passed to the PHP script as a separate array element through the command line, and the other is the $ARGC variable, which is used to hold the elements of the $ARGV array.

Number.

It is very simple to write a piece of code that reads $ARGV and processes the parameters contained in a PHP script. Try the example script in list B to see how it works:

list B<? PHP Print_r ($argv);? > Run This script by passing it some arbitrary values, and check the output:shellArray( [0] = = Test. php[1] = chocolate[2] = 276[3] = Killer tie, dude! )

As you can see from the output, the value passed to the test.php is automatically shown as an array element in the $ARGV. It is important to note that the first argument of a $argvis is always

The name of the script itself.

The following is a more complex example (List C):

List C

<?PHP//Check for all required arguments//first argument are always name of script!if($ARGC! = 4) {  die("Usage:book.php <check-in-date> <num-nights> <room-type>");}//Remove first argumentArray_shift($argv);//get and use remaining arguments$checkin=$argv[0];$nights=$argv[1];$type=$argv[2];Echo"You have requested a$typeThe$nightsNights, checking in on$checkin. Thank for your order! ";?>

Here is an example of its usage:

shell> PHP phpbook.php 21/05/2005 7 single
You are requested a single and 7 nights, checking in on 21/05/2005. Thank for your order!

Here, the script first checks the $ARGC to ensure that the number of independent variables meets the requirements. It then extracts each of the arguments from the $argv and prints them out to the standard output device.

From:http://bbs.phpchina.com/blog-52440-185427.html

How to use and run PHP scripts from the Linux command line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.