Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of php-cli?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the use of php-cli, the article is very detailed, has a certain reference value, interested friends must read it!

1. Basic knowledge

1.1 what is Shell programming?

In Unix, shell is not a simple command interpreter (typically DOS in Windows), but a fully functional programming environment. Shell is part of the operating system that deals with users and can be used to coordinate commands [1]. Programming with Shell can flexibly solve a large number of repetitive tasks, which is very convenient. However, the syntax of Shell is very strange (personal opinion), it is not easy to remember, if only a familiar language can be used to write shell-for example, php-- can quickly develop Shell programs (such as my Preminder daemon), so there is this article, this article uses Linux as an example to illustrate the use of php-cli, other platforms have similar versions.

1.2 what is php-cli?

As I just said, we can use php to develop Shell programs. Some students may ask: "isn't php used to make web pages?" Yes, php can be used to make dynamic web pages, and php was developed for dynamic web pages at the beginning, but in theory, php can be used to do any program, even desktop programs, and php-cli is the supporting environment in which php runs on the command line, that is, we say it can be used to write Shell environment support.

Php-cli is the abbreviation of php Command Line Interface, which, as its name implies, is the interface on which php runs on the command line, which is different from the php environment (php-cgi, isapi, etc.) running on the Web server [2].

In other words, php can not only write foreground web pages, it can also be used to write background programs.

two。 Execute php-cli script

2.1Syntax of php-cli

Of course, it is exactly the same as php, because it is php! It's just that some default parameters are different from php-cgi, such as run time: the default run time for php-cli is infinite, while the default setting for web page php is 30s.

2.2 execute php-cli script

2.2.1. Execute php directly on the terminal

Kangzj@localhost# php- r 'print_r (get_defined_constants ()); '2.2.2. Run the php-cli script file

Kangzj@localhost# php my_script.php

The php file on kangzj@localhost# php-f my_script.php is a general php file, no different. Another way is to instruct the interpreter in the file so that the script can be executed directly on the terminal with ". / test.php." the test.php looks like this:

The copy code is as follows:

#! / usr/bin/php-Q

Add: php's Shell program does not necessarily have a php extension, it can have any extension, or even no extension, just to be clear, I use the php extension.

2.2.3. Execute php-cli script with Cron

Cron is a scheduled execution tool under linux, which can run jobs and periodic jobs, such as backup data, Preminder query PR periodically, etc., without human intervention. Add methods: open / etc/crontab and add:

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

The above is all the content of this article "what's the use of php-cli?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report