A few inconspicuous tricks in php

高洛峰
Release: 2016-11-28 16:24:47
Original
811 people have browsed it

It is said to be an inconspicuous little trick, but in fact it should be said to be a routine application that is not commonly used. Many things are like this. Knowing is one thing, being able to use it is another thing, and practicing it is another thing. .Becoming a master requires solid basic skills.

str_repeat

Relying on it to repeatedly output strings, similar to x in perl

php -r 'echo str_repeat("ABC",5),"n" ;'

ABCABCABCABCABC

substr

This is used to intercept the character device, for example, to intercept the first letter of the string:

$string = 'abcdefg'

substr($string,0,1 ) and got a. But now I am accustomed to using $string[0]. By the way, when judging whether the length of string is 7, Isset($string[6]) is now used instead, because it is said that isset is faster than strlen. Similarly, this experience also applies to count.

trim

trim is used to remove leading and trailing blanks and trailing line breaks. It took a long time to use it, so that the author Because it is specifically designed to do this. I didn't expect that it can also accept a parameter list to remove unwanted characters at the beginning and end, such as %

trim('%abcdef%','%' to remove '%abcdef%' )

continue

This guy is used to skip the following loop. After using it for a long time, I always thought it had no parameters, until one time I wanted to jump out of a three-level loop...

ini_set

When we write programs based on network connections, it is necessary to set the socket timeout in consideration of fault tolerance. The default time defined in php.ini is 60 seconds.

; Default timeout for socket based streams (seconds )

; http://php.net/default-socket-timeout

default_socket_timeout = 60

In the php manual, you can use ini_set to modify the configuration of php.ini, so I thought of:

ini_set('default_socket_timeout',6 );

When I use some newly discovered functions, my habit is:

var_dump(ini_set('default_socket_timeout',6));

A running result prompt:

string(2) "60"

Huh? Did the setting fail? I tried on several machines and the same thing happened. Hey, what should I do? After researching for a long time, I finally found a problem. The PHP manual says this:

Return Values

Returns the old value on success, FALSE on failure.

Hey, I read the manual too carelessly!

posix_kill

nginx’s log rotation script is written in php. In order to update php, I notify nginx to regenerate new logs. Posix_kill:

posix_kill($nginx_pid,SIGUSR1)

On the N machines I used, this function worked normally. But when I lent this script to a buddy, the machine reported:

Warning: posix_kill() expects parameter 2 to be long, string given

Look at the function prototype: bool posix_kill (int $pid, int $sig)

The second parameter must be given to int. Why is SIGUSR1 on my machine? My friend's machine is not working? Is there a problem with the PHP version? My PHP version is higher than mine! I searched online for a long time to find the int value corresponding to SIGUSR1, but I couldn't find it. Finally, I studied the kill command, but it was unintentional. Enter: kill -l and got it.

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE

9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS

13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG

17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD

21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU

25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH

29) SIGINFO 30) SIGUSR1 31) SIGUSR2

The above is for Mac, but the corresponding value of SIGUSR1 for Linux is actually different, I am speechless.

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP

6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1

11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM

16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP

21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR

31 ) SIGSYS 34) SIGRTMIN 35 ) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3

38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8

43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12

53) SIGRTMAX -11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7

58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

63) SIGRTMAX-1 64) SIGRTMAX

What if you want to support different systems at the same time? Add a judgment, php has an artifact called PHP_OS.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!