How to use and run PHP scripts from the Linux command line, linux command line PHP script _php tutorial

Source: Internet
Author: User
Tags php debugging tools

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


The following is an illustrated way to share with you under the Linux command to use and run PHP scripts.

PHP is an open source server-side scripting language, originally three letters representing the "Personal Home page", and now stands for "Php:hypertext preprocessor", which is a recursive acronym. It is a cross-platform scripting language that is heavily influenced by C, C + +, and Java.

Running PHP code on the Linux command line

PHP's syntax is very similar to the syntax of C, Java, and Perl with some PHP features, which is currently used by 260 million web sites, and the latest stable version is PHP version 5.6.10.

PHP is an embedded script for HTML that enables developers to quickly write dynamically generated pages. PHP is primarily used for server-side (while JavaScript is used by clients) to generate dynamic Web pages over HTTP, but you might be surprised when you know you can execute PHP without a Web browser in your Linux terminal.

This article explains 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 the Code code as follows:
# Apt-get Install PHP5-CLI
[Debian and similar systems]# yum install PHP-CLI
[CentOS and similar systems]

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

Copy the 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 browser is required and the same results can be obtained from the Linux terminal. Execute /var/www/html/infophp.php on the Linux command line, such as:

Copy the 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 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:

Copy the Code code as follows:
# php-f/var/www/html/infophp.php | Less

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:

Copy the Code code as follows:
# 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.

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

Copy the 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 PHP interactive mode.

Enable PHP interactive mode

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

Copy the Code code as follows:
# echo-e ' #!/usr/bin/php\n<?php phpinfo ();?> ' > phpscript.php

Notice that we used #!/usr/bin/php The first line of the PHP script, just like 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.

Next, let the script execute:

Copy the Code code as follows:
# chmod 755 phpscript.php

Then run it,

Copy the Code code as follows:
#./phpscript.php

5. You can create simple functions entirely on your own by interacting with the shell, which you are sure to be surprised by. The following is a step-up guide.

Turn on PHP interactive mode.

Copy the Code code as follows:
# PHP-A

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

Copy the Code code as follows:
PHP > function addition ($a, $b)

Use curly braces to define a rule for the function in the meantime.

Copy the Code code as follows:
php > {

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

Copy the Code code as follows:
PHP {echo $a + $b;

All rule definitions are complete and the rules are encapsulated by closing curly braces.

Copy the Code code as follows:
PHP {}

To test the function, add the numbers 4 and 3 with the following command:

Copy the Code code as follows:
php > Var_dump (addition (4,3));

Sample output

Copy the Code code as follows:
7NULL

You can run the following code to execute the function, you can test the different values, you want to think how many times the line. Replace the A and B inside to your own value.

Copy the Code code as follows:
php > Var_dump (addition (a, b));
php > Var_dump (addition (9,3.3));

Sample output

Copy the Code code as follows:
12.3NULL

Creating PHP Functions

You can run the function until you exit interactive mode (CTRL + Z). Also, you should notice that the data type returned in the output above is NULL. This problem can be fixed by requiring the PHP interactive shell to replace echo return results with return.

You only need to replace the ' echo ' declaration with ' return ' in the function above.

Replace

Copy the Code code as follows:
PHP {echo $a + $b;

For

Copy the Code code as follows:
PHP {return $a + $b;

The rest 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 kept from a shell session to the next shell session, so once you exit the interactive shell, it is lost.

The above content is to use and run all the contents of PHP script through the Linux command line, I hope everyone likes it.

http://www.bkjia.com/PHPjc/1039192.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039192.html techarticle How to use and run PHP scripts from the Linux command line, under the Linux command line PHP script to share with you through the Linux command to use and run PHP scripts. PHP is a ...

  • 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.