How to use and execute PHP code in Linux command lines

Source: Internet
Author: User
Tags add numbers
PHP is an open-source server-side scripting language. at first, these three letters represent "PersonalHomePage", but now they represent "PHP: HypertextPreprocessor"

PHP is an open-source server-side scripting language. at first, these three letters represent "Personal Home Page", but now they represent "PHP: Hypertext Preprocessor ", it is the abbreviation of recursion. It is a cross-platform scripting language that is deeply influenced by C, C ++, and Java.

Run the PHP code in the Linux command line

The syntax of PHP is very similar to that of C, Java, and Perl with some PHP features. it is currently used by about 0.26 billion websites, the latest stable version is PHP 5.6.10.

PHP is an embedded HTML script that allows developers to quickly write dynamically generated pages. PHP is mainly used on the server side (while Javascript is used on the client side) to generate dynamic web pages through HTTP. However, when you know that PHP can be executed on a Linux terminal without a web browser, you may be surprised.

This article describes the command line of the PHP script language.

1. after installing PHP and Apache2, we need to install the PHP command line interpreter.

# Apt-get install php5-cli [Debian and similar systems] # yum install php-cli [CentOS and similar systems]

Next, we usually want to create a content in the location of/var/www/html (which is the working directory of Apache2 in most releases) Run the following command to test the file infophp. php (whether PHP is correctly installed.

# echo '
 ' > /var/www/html/infophp.php

Then, access http: // 127.0.0.1/infophp. php in the browser, which will open the file in the web browser.

Check PHP information

You can obtain the same result on a Linux terminal without any browser. Run/var/www/html/infophp. php in the Linux command line, for example:

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

Check PHP information from the command line

Because the output result is too large, we can send the above output result to the less command through the pipeline, so that we can output a screen at a time. the command is as follows:

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

Check all PHP information

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

2. you can directly use phpinfo (), a very valuable debugging tool, in the Linux command line without calling it from a file. you only need to execute the following command:

# php -r 'phpinfo();'

PHP debugging tool

Here, the '-R' option will remove the PHP code from the Linux Terminal <和> Mark for direct execution.

3. run PHP in interactive mode and perform some mathematical operations. Here, the '-a' option is used to run PHP in interactive mode.

# php -a Interactive shellphp > echo 2+3; 5 php > echo 9-6; 3 php > echo 5*4; 20 php > echo 12/3; 4 php > echo 12/5; 2.4 php > echo 2+3-1; 4 php > echo 2+3-1*3; 2 php > exit

Enter 'exit 'or press 'Ctrl + C' to disable the PHP interaction mode.

Enable PHP interaction mode

4. you can run the PHP script as a shell script. First, create a PHP sample script in your current working directory.

# echo -e '#!/usr/bin/php/n
 ' > phpscript.php

Note: We use this PHP script in the first line #! /Usr/bin/php, just like in shell scripts (/bin/bash ). # Of the first line #! /Usr/bin/php tells the Linux command line to use the PHP interpreter to parse the script file.

Next, let the script be executable:

# chmod 755 phpscript.php

Then run it,

# ./phpscript.php

5. You can create simple functions on your own through interactive shell, which will surely surprise you. The following is a step-by-step guide.

Enable PHP interaction mode.

# php -a

Create a function and name it addition. At the same time, declare two variables $ a and $ B.

php > function addition ($a, $b)

Use curly brackets to define rules for the function.

php > {

Define rules. Here, this rule is about adding these two variables.

php { echo $a + $b;

After all rules are defined, wrap the rules by closing curly braces.

php {}

To test the function, add numbers 4 and 3. the command is as follows:

php > var_dump (addition(4,3));
Sample output
7NULL

You can run the following code to execute the function. you can test different values. you can run the following code as many times as you want. Replace a and B with your own values.

php > var_dump (addition(a,b));
php > var_dump (addition(9,3.3));
Sample output
12.3NULL

Create a PHP function

You can run this function until you exit the interactive mode (ctrl + z ). At the same time, you should also note that the data type returned in the above output result is NULL. This problem can be solved by requiring php interactive shell to replace echo returned results with return.

You only need to use 'Return 'to replace the 'Echo' statement in the above function.

Replace

php { echo $a + $b;

Is

php { return $a + $b;

The rest is still the same as the principle.

Here is an example. the correct data type is returned in the output result of this example.

PHP functions

Always remember that user-defined functions are not retained from a shell session to the next shell session. Therefore, once you exit the interactive shell, it will be lost.


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.