Home > php教程 > PHP开发 > body text

PHP training tutorial, several inconspicuous tips of PHP

巴扎黑
Release: 2016-11-07 16:33:51
Original
1513 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

 It relies on it to repeatedly output strings, similar to x in perl

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

  ABCABCABCABCABC

 substr

 This is used to intercept the character device. For example, if you want to intercept the first letter of the string:

 $string = 'abcdefg'

 substr($string,0,1) will get 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 better than strlen It’s fast. In the same way, this experience also applies to count.

 trim

 trim is used to remove leading and trailing blanks and trailing line breaks. I have been using it for a long time, so much so that the author just does it specifically. This. Unexpectedly, it can also accept a parameter list to remove unwanted characters at the beginning and end, such as removing the % of '%abcdef%'

  trim('%abcdef%','%')

 continue

This guy is used to skip the following loops. 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

We are writing When considering fault tolerance for programs based on network connections, it is necessary to set the socket timeout. 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);

The author is using some new discoveries When using the function, the habit is:

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

 A running result prompt:

 string(2) "60"

 咦? Did the setting fail? Change a few machines The machine test is still like this. Hey, what should I do? After studying for a long time, I finally found a problem, which is written in the PHP manual:

  Return Values ​​

 Returns the old value onsuccess, FALSE on failure.

  Hey, it’s too much to read the manual I'm not careful!

posix_kill

 I wrote nginx's log rotation script in php. In order to update php, I used posix_kill when notifying nginx to regenerate new logs:

posix_kill($nginx_pid,SIGUSR1)

  This function works normally on the N machines I use. However, 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 int. Why does SIGUSR1 not work on my machine but not on my buddy’s machine? PHP version problem ?Brother’s PHP version is actually 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 and accidentally entered: kill -l and got it.

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

5) SIGTRAP 6) SIGABRT 7)SIGEMT SIGFPE

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

13) SIGPIPE 1 4) SIGALRM 15) SIGTERM 16) SIGURG

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

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

25) SIGXFSZ 26) SIGVTALRM27) SIGPRO F 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) SIGBUSSIGFPE 9) SIGKILL 10) SIGUSR1

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

16) SIGSTKFLT 17) SIGCHLD18) SIGCONT 19) SIGSTOP 20) SIGTSTP

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

26) SIGVTALRM 27) SIGPROF28) 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+1550) 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 


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template