Some experiences in writing the php command line program recently

Source: Internet
Author: User
In the linux world, it is very glorious to be able to accomplish a mission together with other small programs ~ How can we do this? Php, php, yes, or php. four articles related to php have been written. (< <浅谈php脚本> >,< >,< <用php操作gsm modem发短信> >,< <让你的php命令行程序处理管道数据 > >) But I still don't think it's good. I still want to summarize some of my experiences in using php to write the command line program over the past six months. I hope that I will be able to improve myself in the review process.
A brief review of what has been written:
1. run the php program on your own.
The php mentioned here, 'self-execute ', does not compile php into binary code, so it has executable permissions. it means to add a file header to php :#! /Usr/bin/php, and then execute permissions to the file through chmod, so that you do not need to add a php before the file every time you want to execute it. for details, see < <浅谈php脚本> >.
2. Let your program get out of the terminal currently occupied.
If your program runs for a long time, or you just need to run it all the time, in order to put it in the background, we will add one at the end and put it in the background when running it. or run screen or tmux and then run it in it. however, these practices are always very argy. now, a better solution is to let it put itself in the background. for details, see < >.
3. in addition to manual data input, you can also receive data from others.
In the linux world, it is very glorious to be able to accomplish a mission together with other small programs ~ How can we do this? For details, see: < <让你的php命令行程序处理管道数据 > >
There is a saying: 'Learn and learn new, can be a teacher Yi '. I found that I didn't know new things. I think the current php script technology is very mature and complete, it is easy to learn and use. when using php to write a command line script, I mainly use it in two directions: background services and command line programs. the background service is always running on the server and runs in memory, so pay special attention to code optimization. some of the things in the command line program can be directly applied to the background service, OK, start:
Background service:
1. running in the background
I already agreed that it is a background service. do you still want to use the current terminal? Go to the background :)
2. long resident memory
First, the program must run for a long time. it seems that it can only be placed in the while (0. when I was preparing to use this method, I read a lot of information on the internet and do not advocate this method for php. but what should I do without this method? Goto? It is a feature later than 5.3, and now the server stresses that the stability is mostly in Version 5.2, and this forces the code to jump off and quit. do you dare to use it? Finally, I ran the test server for more than a month. it felt very good and powerful!
Second, we have to raise the issue of memory release when talking about long-standing memory. php does not have a memory recovery mechanism. although it does not need to allocate and clean up the memory like c, it is better to pay attention to it and be careful when you get to the ship. therefore, in terms of memory usage, I stick to the principle that the memory in the function is unset when the memory in the main program is used up. this can also avoid unexpected effects caused by variable name duplication. (Of course, php seems to have the same variable name. who will take effect later, but it is very important to develop a good habit :))
3. disable output
Does disabling output mean disabling echo? Yes, but not all! There are two types of output in the linux World: normal output and error output. normal output. you don't have to say it. if you write the best code, an echo will immediately return. in addition, the error output must be restrained! For example, mysql_connect. the command execution result is successful or failed. in most cases, it is easy to succeed. as a result, many people naturally think that this function will succeed! As a result, you will see a lot of Web pages prompt mysql connection failure information when errors occur. the webpage is okay, because it is a person once, this person fails, maybe the next person will be successful, but the background services are different. if you do not handle these errors, the program will go back directly! Therefore, to ensure the robustness of the program, we must understand how to suppress errors. Furthermore, considering the comprehensiveness is also a mature performance of men :)
4. signal control
How can a complete background program end with a brute force method like 'kill-9 pid? There are also perfect signal solutions in php, so that your program supports start, stop, restart, and status.
5. run logs
Background program. if output is disabled, logs must be written. detailed records must be recorded from startup to what is going on, and when to exit for any reason. when I still don't understand the signal, the log only contains the startup and running records, and then the startup and running records, later, I realized why the program exited. :) When I was writing logs, I found a very handsome function file_put_contents, and she supported append. in this way, I can use the 'tail-f log name' to view the running status in real time :)
Command line program:
6. echo is crazy.
As mentioned in php best practices, echo is much faster than print because there is no returned value. echo can be followed by commas to separate multiple sentences. the input efficiency is much faster than the point connection. so when I want to process a large amount of text and want to write them to a file for the last time, I wish I could not use it '. 'to connect these things, because I don't want to waste time on the connection string! Php told me that you can do it! So I was pleasantly surprised to find that ob_start is okay! For example:
Ob_start;
Echo xxx, xxx, xxxx;
File_put_contents ('filename', ob_get_contents );
Ob_clean;
Very pornographic and violent! Who knows the effect.
7. correct time zone errors
Installing a linux system is quite simple. however, I am very depressed that many people can install the linux system into a Chinese version (because the installation language has chosen Chinese), but it turns the time into the New York time (the West eight district, China is the East eight district ). so as a programmer, don't expect everything to be right by default. to ensure that your program is not faulty, you 'd better define your own time zone when writing logs or outputting Time: date_default_timezone_set ('prc '):)
Well, it can be simply written here. a short summary and review will continue to be made and used for php :)

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.