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

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
