PHP-CLI Introduction (Shell language) _php tips

Source: Internet
Author: User
Tags learn php php cli
1. Basic knowledge

1.1 What is shell programming?
In Unix, the shell is not a simple command interpreter (typically with DOS in Windows), but rather a full-featured programming environment. The Shell is part of the operating system that is used to deal with users and can be used to coordinate the commands "1". Shell programming can be flexible to solve a large number of repetitive tasks, very convenient. However, the shell's syntax is very bizarre (personal opinion), it is not easy to remember, if the familiar language can be used to write a shell that would be good-for example, php--can quickly develop the shell program (such as my Preminder background program), so I have this article, This article takes Linux as an example to illustrate the use of PHP-CLI, and other platforms have similar versions.

1.2 What is PHP-CLI?
Just said, we can use PHP to develop shell programs. Some students may ask: "PHP is not used to do the Web page?" -_-”。 Yes, PHP can be used to do dynamic Web pages, and the original PHP is to do Dynamic Web page development of the language, but theoretically PHP can be used to do any program, even desktop programs, and PHP-CLI is the PHP command line to run the support environment, That's what we say about environment support that can be used to write shells.

PHP-CLI is the abbreviation for PHP command line interface, which, as its name means, is the interface that PHP runs on the command-line, different from the PHP environment (PHP-CGI, ISAPI, etc.) running on the Web server "2".

In other words, PHP can not only write the front page, it can also be used to write the background of the program.

2. Execute PHP-CLI Script

The Grammar of 2.1 php-cli
Of course it's exactly the same as PHP, because it's PHP! But some default parameters differ from php-cgi, such as elapsed time: PHP-CLI default run time is infinite, and web php default is 30s.

2.2 Execute PHP-CLI Script

2.2.1. Execute PHP directly at terminal
kangzj@localhost# php-r ' Print_r (get_defined_constants ()); ' 2.2.2. Run php-cli script file
kangzj@localhost# PHP my_script.php

kangzj@localhost# php-f my_script.php on the php file is the general PHP file is no different. There is also a way in which a directive interpreter in a file can be executed directly at the terminal with "./test.php", test.php as follows:

Copy Code code as follows:

#!/usr/bin/php-q
<?php
echo "Hello World of PHP cli!";
?>


Add: PHP Shell program does not necessarily use PHP as an extension, can be any extension, or even do not extend the name, just to be clear, I used the PHP extension.

2.2.3. Execute php-cli script with cron
Cron is a regular execution tool under Linux, you can run the job without human intervention, periodic job, such as backup data, preminder regular query PR, etc., add the method: Open/etc/crontab, add:

0 * * */usr/bin/php-f/home/phpscripts/phpcli.php

4. Conclusion
If you can PHP, then you will also have a shell programming language!

If you do not PHP, you go to learn PHP, is equivalent to Learning Dynamic Web page and Shell two languages! And you can even use PHP to write a graphical interface of the application, dnspod dynamic Domain name client is one of the development in PHP.

PHP's easy to learn is famous, if you do not, what is the hesitation?

In addition, to publicize my "preminder"--PR Update email alert Service ~ ~

5. Reference Documents
1.Linux Shell Introduction: http://www.jb51.net/article/37801.htm
2.PHP Command Line Interface:mystic unleashed:http://www.php-cli.com/
3.ch 4.2, PHP manual:http://www.php.net
Ps:shell command still need to know some, otherwise some functions are not very good implementation.

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.