php, php, yes, it is still php. I have written four articles related to php. (>, >, >, >) But I still feel that it is not enough, and I still want to use php for half a year. Summary of the experience of writing command line procedures, I hope that in the process of reviewing, I have also been sublimated and improved.
Simply review what I have written:
1. Let the php program execute itself
PHP, ' Executing it yourself' does not mean compiling PHP into binary code so that it has executable permissions. It means adding a file header to PHP: #!/usr/bin/php, and then giving the file execution permissions through chmod, so as not to have to execute it every time. In order to execute it every time, add a php in front of the file. For details, see >.
2. Let your program give up the terminal currently occupied.
If your program takes a long time to run, or simply wants to keep running Well, in order to put it in the background before, when we ran it, we would add an & at the end to put it in the background. Or run a screen or tmux and then run it in it. However, these methods always feel very awkward. argy, now there is a better solution, which is to let it put itself in the background. For details, see >.
3. In addition to manually inputting data, you can also receive data from others.
In the Linux world, It is a great honor to be able to complete a mission together with other small programs ~ How can we do it? For details, see: >
There is a saying: 'Reviewing the past and learning the new can teach others.' Let's review the past and learn the new. After a while, I found that I didn’t know anything new, so let’s continue talking about new things:) Personally, I feel that the current PHP scripting technology is very mature and complete, and it is easy to learn and use, and it is easy to get started. When using PHP When writing command line scripts, I mainly use them in two directions: background services and command line programs. Because background services have to be running on the server side and resident in memory, special attention must be paid to the optimization of the code. In command line programs Some stuff can be directly applied to background services, ok, let’s start practicing:
. . The first point of the memory, The first point, the program should run for a long time, it seems that it can only be placed in a while (0). When I was preparing to use this method before, I read many information on the Internet. What should I use for this method? goto? That is a feature after 5.3, and now that the server emphasizes stability, most users use the 5.2 version. And do you dare to use such an instruction that forces you to jump around in the code? In the end, use while. . I have been running the test server for more than a month, and I feel very good and powerful!
The second point, when it comes to permanent memory, we have to mention the issue of memory release. PHP does not have a memory recycling mechanism, although it is not like C, where you have to allocate and Clean up the memory, but it is better to pay attention to it. Be careful and you will sail forever. Therefore, when it comes to memory usage, the author adheres to a principle: no matter the memory in the function, unset the memory in the main program when it is used up. This can also avoid the duplication of variable names. Bring unexpected effects. (Of course, PHP can seem to have variables with the same name, whoever comes later will take effect, but it is important to develop a good habit:))
3. Disable output
?Yes, but not entirely correct! Because there are two types of output in the Linux world, one is normal output and the other is error output. Needless to say, normal output, no matter how well written the code is, an echo will be returned immediately. .Then there is the error output, errors must be suppressed! For example: mysql_connect. There are two results of executing this command: success and failure. In most cases, it is easy to succeed. So many people take it for granted. , this function will definitely succeed! So you will see many web pages prompting the message that mysql connection failed when an error occurs. The web page is okay, because it is one person at a time, and this person failed, maybe it will succeed when the individual is placed. , but the background service is different. If you don’t handle these errors, the program will simply exit! Therefore, for the robustness of the program, you must know how to suppress errors. Furthermore, considering everything comprehensively is also a sign of maturity for a man:)
. Signal control 完 A complete background program, how can it be eased by violent ways such as the "Kill -9 PID '? PHP also has a perfect signal solution. Let your program support Start, Stop, RESTART, Status, Status It’s just a random thing.
5. Operation log
If the background program disables output, it must write logs. From startup to what it is doing, to when it exits for what reason, there must be detailed records. Before I understood the signal, the log There are only startup and running records, and then there are startup and running records, which is frustrating. Later, I can finally know when the program exited:) While writing the log, I found a very cool function file_put_contents, and she supports appending. In this case, I can use 'tail -f log name' to see the running status in real time:)
Command line program:
6. Echo is also crazy
It is mentioned in php best practices: echo has no return value, so it is better than Print is much faster. You can use commas to separate multiple sentences after echo, and the input efficiency is much faster than using dot connections. So, when I want to process a large amount of text and want to write it to a file for the last time, I How I wish I could connect these things without '.', because I don't want to waste time on connection strings! PHP told me that you can do it! So I was pleasantly surprised to find that ob_start can! For example:
ob_start;
echo xxx ,xxx,xxxx;
file_put_contents('filename',ob_get_contents);
ob_clean;
Very violent! The effect is just right, who knows who uses it.
7. Correcting time zone errors
Installing a Linux system is very simple. .But what makes the author very depressed is that many people can install the Linux system into the Chinese version (because the installation language is Chinese), but they change the time to New York time (West Eighth District, China is East Eighth District). So as Programmers, don't expect everything to be correct by default. In order to ensure that your program does not have problems, it is best to define the time zone yourself when writing logs or outputting time: date_default_timezone_set('PRC') :)