How to Use and run PHP scripts and Linux command-line php scripts through linux commands
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.
Copy codeThe Code is as follows:
# Apt-get install php5-cli
[Debian and similar systems] # yum install php-cli
[CentOS and similar systems]
Next, we usually want to do the following:/Var/www/html(This is the working directory of Apache2 in most releases ).<? Php phpinfo ();?>, NamedInfophp. phpTo test (PHP installation is correct), run the following command.
Copy codeThe Code is as follows:
# Echo '<? Php phpinfo ();?> '>/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. phpSuch:
Copy codeThe 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 resultless
Command to output a screen at a time. The command is as follows:
Copy codeThe 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. We can directly usephpinfo()
This very valuable debugging tool does not need to be called from a file. You only need to execute the following command:
Copy codeThe Code is as follows:
# Php-r 'phpinfo ();'
PHP debugging tool
Here, the '-R' option will remove the PHP code from the Linux terminal<
And>
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.
Copy codeThe 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.
Copy codeThe Code is as follows:
# Echo-e '#! /Usr/bin/php \ n <? Php phpinfo ();?> '> Phpscript. php
Note that we use#!/usr/bin/php
, As in shell scripts(/bin/bash
). The first line#!/usr/bin/php
Tell the Linux Command Line to use the PHP interpreter to parse the script file.
Next, let the script be executable:
Copy codeThe Code is as follows:
# Chmod 755 phpscript. php
Then run it,
Copy codeThe 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.
Copy codeThe Code is as follows:
# Php-
Create a function and name itaddition
. At the same time, declare two variables$a
And$b
.
Copy codeThe Code is as follows:
Php> function addition ($ a, $ B)
Use curly brackets to define rules for the function.
Copy codeThe Code is as follows:
Php> {
Define rules. Here, this rule is about adding these two variables.
Copy codeThe Code is as follows:
Php {echo $ a + $ B;
After all rules are defined, wrap the rules by closing curly braces.
Copy codeThe Code is as follows:
Php {}
To test the function, add numbers 4 and 3. The command is as follows:
Copy codeThe Code is as follows:
Php> var_dump (addition (4, 3 ));
Sample output
Copy codeThe 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.
Copy codeThe Code is as follows:
Php> var_dump (addition (a, B ));
Php> var_dump (addition (9, 3.3 ));
Sample output
Copy codeThe 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
Copy codeThe Code is as follows:
Php {echo $ a + $ B;
Is
Copy codeThe 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.