How to use and run a PHP script _php instance through the Linux command line

Source: Internet
Author: User
Tags install php php and php code php debugging tools php script phpinfo

The following is an illustrated way to share the use and running of PHP scripts through Linux commands.

PHP is an open source server-side scripting language, which initially represents the "Personal home Page", and now represents "Php:hypertext preprocessor", which is a recursive acronym. It is a cross-platform scripting language, deeply influenced by C, C + +, and Java.

Running PHP code on the Linux command line

The syntax of PHP and C, Java, and Perl with some PHP features are very similar to the syntax in language, which is currently used by about 260 million websites, and the latest stable version is the PHP version 5.6.10.

PHP is an HTML embedded script that allows developers to quickly write dynamically generated pages. PHP is primarily used on the server side (while JavaScript is used for clients) to generate dynamic Web pages via HTTP, but you might be surprised when you know you don't need a Web browser to execute PHP in a Linux terminal.

This article describes the command-line aspects of the PHP scripting language.

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

Copy Code code as follows:

# Apt-get Install PHP5-CLI
[Debian and similar systems]# yum install PHP-CLI
[CentOS and similar systems]

What we usually do next is to create a content for <?php phpinfo () in/var/www/html (this is the working directory of Apache2 in most distributions);?>, named infophp.php files to test (PHP is installed correctly), execute the following command.

Copy Code code as follows:

# echo ' <?php phpinfo ();?> ' >/var/www/html/infophp.php

The browser then accesses http://127.0.0.1/infophp.php, which opens the file in a Web browser.

Check PHP information

No browsers are required, and the same results can be achieved in Linux terminals. Perform /var/www/html/infophp.php on the Linux command line, such as:

Copy Code code as follows:

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

Checking PHP information from the command line

Because the output is too large, we can channel the output to the less command, so that we can output one screen at a time, the command is as follows:

Copy Code code 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 use this very valuable debugging tool directly on the Linux command line without phpinfo() needing to invoke it from a file, just execute the following command:

Copy Code code as follows:

# php-r ' phpinfo (); '

PHP Debugging Tools

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

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

Copy Code code 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 turn off the PHP interactive mode.

Enable PHP interactive mode

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

Copy Code code as follows:

# echo-e ' #!/usr/bin/php\n<?php phpinfo ();?> ' > phpscript.php

Notice that we use #!/usr/bin/php the first line of the PHP script, as in the shell script ( /bin/bash ). The #!/usr/bin/php first line tells the Linux command line to parse the script file with the PHP interpreter.

Second, make the script executable:

Copy Code code as follows:

# chmod 755 phpscript.php

Then run it,

Copy Code code as follows:

#./phpscript.php

5. You can create simple functions on your own by interacting with the shell, and you'll be surprised. Here is a step-by-step guide.

Open the PHP interactive mode.

Copy Code code as follows:

# PHP-A

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

Copy Code code as follows:

PHP > function addition ($a, $b)

Use curly braces to define the rules for the function.

Copy Code code as follows:

php > {

Define the rule. Here, the rule is about adding these two variables.

Copy Code code as follows:

PHP {echo $a + $b;

All rules are defined and the rules are encapsulated by closing curly braces.

Copy Code code as follows:

PHP {}

To test the function, add the numbers 4 and 3, and the commands are as follows:

Copy Code code as follows:

php > Var_dump (addition (4,3));

Sample output

Copy Code code as follows:

7NULL

You can run the following code to execute the function, you can test different values, you want to do as many times as possible. Replace A and b inside with your own values.

Copy Code code as follows:

php > Var_dump (addition (A,B));
php > Var_dump (addition (9,3.3));

Sample output

Copy Code code as follows:

12.3NULL

Creating PHP Functions

You can always run the function until you exit the interactive mode (CTRL+Z). At the same time, you should also note that the data type returned in the output above is NULL. This problem can be fixed by requiring the PHP interaction shell to return the result with the returns instead of ECHO.

Only the ' echo ' declaration in the function above should be replaced with ' return '

Replace

Copy Code code as follows:

PHP {echo $a + $b;

For

Copy Code code as follows:

PHP {return $a + $b;

The rest of the stuff is still the same as the principle.

Here is a sample that returns the correct data type in the output of the example.

PHP functions

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

The above content is through the Linux command line to use and run the entire content of PHP script, I hope you like.

Related Article

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.