How to Use and run PHP scripts through Linux commands

Source: Internet
Author: User
Tags add numbers
This article mainly introduces how to use Linux commands to execute and run PHP scripts. The syntax in the PHP language is very similar to that in the language of Perl, which has some PHP features of CJava and runtime, the current version is stable and the latest version is 5.

This article mainly introduces how to use Linux commands to run and execute PHP scripts. The syntax in the PHP language is very similar to that in the C/Java language and the Perl language with some PHP features in runtime, the current version is stable and the latest version is 5.

Below we will share with you how to use and run PHP scripts through Linux commands.

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.

The Code is as follows:


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

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

The Code is as follows:


# Echo '<? Php phpinfo ();?> '>/Var/www/html/infophp. php

Then, access the file through 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:

The Code is as follows:


# 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:

The Code 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:

The Code is as follows:


# Php-r 'phpinfo ();'

PHP debugging tool

Here, the '-R' option will allow PHP code to be executed directly without the <and> flag in the Linux terminal.

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

The Code is as follows:


# Php-aInteractive shellphp> echo 2 + 3; 5php> echo 9-6; 3php> echo 5*4; 20php> echo 12/3; 4php> echo 12/5; 2.4php> echo 2 + 3-1; 4php> echo 2 + 3-1*3; 2php> 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.

The Code is as follows:


# Echo-e '#! /Usr/bin/php \ n <? Php phpinfo ();?> '> 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:

The Code is as follows:


# Chmod 755 phpscript. php

Then run it,

The Code is as follows:


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

The Code is as follows:


# Php-

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

The Code is as follows:


Php> function addition ($ a, $ B)

Use curly brackets to define rules for the function.

The Code is as follows:


Php> {

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

The Code is as follows:


Php {echo $ a + $ B;

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

The Code is as follows:


Php {}

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

The Code is as follows:


Php> var_dump (addition (4, 3 ));

Sample output

The Code is as follows:


7 NULL

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.

The Code is as follows:


Php> var_dump (addition (a, B ));
Php> var_dump (addition (9, 3.3 ));

Sample output

The Code is as follows:


12.3 NULL

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

The Code is as follows:


Php {echo $ a + $ B;

Is

The Code is as follows:


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.

The above content is all about using and running PHP scripts through Linux commands. I hope you will like it.

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.