A few inconspicuous tricks in php
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
