当前位置:网站首页>Basic knowledge of PHP - complete collection of PHP functions
Basic knowledge of PHP - complete collection of PHP functions
2022-07-24 09:24:00 【Chon.Wang】
This article sorts out some commonly used functions , And classified . If it helps you , Yes , Bloggers are very blunt to Three even
One 、 Array function
#1. Array creation
array(); # Generate an empty array
array_combine($x, $y); # Generating arrays , use $x The value of the array is used as the Key name , $y The value of the array is the value of the new array value
#2. Array merging and splitting
array_merge($x, $y, $z); # Array merge
array_slice($array, $start, $length); # Custom location interception
#3. Array comparison
array_diff($x, $y, $z); # Return difference set
array_intersect($x, $y, $z); # Return to intersection
#4. Find and replace arrays
array_search($search, $array); # Find value , There is a return key , There is no return false
array_splice($search, $start, $length, $new); # from $search Remove from array , And use $new Chinese and new elements replace
array_sum(); # Returns the sum of all the values in the array
array_key_exists($key, $array); # Check $key Whether the key name of is $array in
in_array($val, $array); # Check $val Is the value of $array in
#5. Array key operation
count(); # Get the number of elements in the array
array_unique(); # Array value de duplication , Delete duplicate elements , Returns the remaining array
array_rand($array, $length); # Randomly extract from an array $length Elements , Be careful : The return is Key name
array_keys(); # Put all the keys in the array , Make a new array
array_values(); # All the values in the array , Make a new array
array_flip(); # Invert the keys and values of the array
array_reverse(); # Return the array of flipping order
array_count_values(); # Count the number of occurrences of all values in the array ['A'=>3, 'B'=>4]
shuffle($array); # Scramble the elements in the array , Rearrange randomly
#6. Array pointer operation
key(); # Returns the key name of the current element of the internal pointer of the array
current(); # Returns the value of the current element of the internal pointer of the array
next(); # Move the pointer to the current element back one bit , And return the value of the element moved back
prev(); # Move the pointer to the current element forward one bit , And return the value of the element moved forward
end(); # Point the array pointer to the last element , And return the value of the element
reset(); # Point the array pointer to the first element , And return the value of the element
array_shift(); # Delete the first element in the array , And return the value of the deleted element
array_unshift($array, $value); # Insert one or more elements at the beginning of the array
array_pop(); # Delete the last element in the array .
array_push($array, $value); # # Insert one or more elements at the end of the array
list(); # Assign values to a group of variables using elements in an array example : list($a, $b, $c) = ['a', 'b', 'c'];
#7. Array sorting
sort(); # Sort the values of the array in ascending order , Do not keep the original key name
rsort(); # Sort the values of the array in descending order , Do not keep the original key name
asort(); # Sort the values of the array in ascending order , Keep the original key name
arsort(); # Sort the values of the array in descending order , Keep the original key name
ksort(); # Sort the keys of the array in ascending order , Keep the original key name
krsort(); # Sort the keys of the array in descending order , Keep the original key name
natsort(); # Use natural order algorithm to sort the values in the array , Keep the original key name
natcasesort(); # Use natural order algorithm to sort the values in the array , Keep the original key name , The function is case insensitive
Two 、 Mathematical functions
abs(); # Returns the absolute value of a number 3.2
ceil(); # Rounding up
floor(); # Rounding down
fmod(5.4, 1.3); # Floating point remainder 0.2
max(3, 1, 5, 7); # Maximum 7
min(4, 2, 6, 8); # minimum value 2
mt_rand(1, 10); # My random value this time is 382819
pow(x, y); # return x Of y Power example : pow(2,2); Output 4
pi(); # Get the pI value , The precision of the return value is determined by php.ini Medium precision Command control , 3.1415926535898
round(); # Floating point rounding
sqrt(16); # Returns the square root of a number 4
3、 ... and 、 Character functions
# 1. String statistics
strlen(); # Statistical string length , Chinese accounts for 3 Bytes
mb_strlen(); # Statistical string length , Chinese accounts for 1 Bytes
str_word_count($string, $format, $characters); # Count the number of words in the string , Please refer to the manual for more usage
# 2. String search and replacement
str_replace($new, $old, $string); # String substitution operation , Case sensitive
str_ireplace($new, $old, $string); # String substitution operation , Case insensitive
substr_replace($string, $replace, $start, $length); # Replace one part with another string
substr_count($string, $search); # Count the number of times one string appears in another string
similar_text($string1, $string2, $percent); # Returns two strings with the same number of strings or the same percentage value
strstr($string, $search, $before=false); # Find where a string first appears in another string , And returns all characters from this position to the end of the string or the beginning of the string , No return FALSE, Case sensitive
stristr($string, $search, $before=false); # Find where a string first appears in another string , And returns all characters from this position to the end of the string or the beginning of the string , No return FALSE, Case insensitive
strchr($string, $search, $before=false); # strstr(); Another name for
strrchr($string, $search); # Find the last occurrence of a string in another string , And returns all the characters from that position to the end of the string , No return FALSE, Case sensitive
strpos($string, $search, $start); # Returns the position where the string first appears in another string , Case sensitive
stripos($string, $search, $start); # Returns the position where the string first appears in another string , Case insensitive
strrpos($string, $search, $start); # Returns the last occurrence of a string in another string , Case sensitive
strripos($string, $search, $start); # Returns the last occurrence of a string in another string , Case insensitive
strspn($string, $charlist, $start, $length); # Back in the $string Contained in the $charlist The number of characters specified in the parameter .
strcspan($string, $char, $start, $length); # The function returns before any specified character is found , The number of characters to find in the string ( Including Spaces ).
# 3. Character segmentation and splicing
explode($separator, $string); # Press $separator Separate $string , Break it up and reassemble it into an array
implode($separator, $array); # use $separator Separate each value in the array , Convert to new string
substr($string, $start, $length); # Chinese accounts for 3 Bytes , stay $string In a string , from $start Position start , return $length Length string , $length Default until the end of the string
mb_substr($string, $start, $length); # Chinese accounts for 1 Bytes , stay $string In a string , from $start Position start , return $length Length string , $length Default until the end of the string
chunk_split($string, $length, $separator); # Press the string $length The length is divided into small pieces , With $separator Separate , Returns the new string
str_split($string, $length); # Press the string $length The length is divided into small pieces , Returns an array of
# 4. String comparison ( The two strings are the same and return 0)
strcmp($string1, $string2); # Compare two strings , Case sensitive
strcasecmp($string1, $string2); # Compare two strings , Case insensitive
strncmp($string1, $string2, $length); # Before comparing two strings $length length , Case sensitive
strncasecmp($string1, $string2, $length); # Before comparing two strings $length length , Case insensitive
strnatcmp($string1, $string2); # Use " natural " Algorithm to compare two strings , Case sensitive
strnatcmp($string1, $string2); # Use " natural " Algorithm to compare two strings , Case insensitive
# 5. toggle case
strtolower($string); # All strings are converted to lowercase
strtoupper($string); # All strings are capitalized
lcfirst($string); # The first letter of the string is lowercase
ucfirst($string); # The initial of a string is uppercase
ucwords($string); # String the first letter of each word is capitalized
# 6. Generation and transformation
strrev(); # Reverse string
str_repeat($string, $multiplier); # Duplicate string $multiplier Time
str_pad($string, $length, $pad_string, $pad_type); # Fill the string with the specified length , $pad_string Characters to be filled , $pad_type For the type (STR_PAD_RIGHT: Fill right ( Default ) STR_PAD_LEFT: Fill left STR_PAD_BOTH: Fill both sides )
wordwrap($string, $length); # Wrap the string by the specified length
str_shuffle(); # Randomly scrambling strings
number_format(); # Thousands of formatted numbers
parse_str(); # Parse the string into variables example : parse_str("name=Chon&height=175");
trim($string, $char); # Remove at the beginning and end $char character , $char Default is space
ltrim($string, $char) # Remove the at the beginning $char character , $char Default is space
rtrim($string, $char) # Remove the at the end $char character , $char Default is space
# 7. html Tag associated functions
htmlentities(); # Convert a character to html Entity
html_entity_decode(); # hold html Entity to character
htmlspecialchars(); # Predefined characters (<>'"&) turn html Entity
htmlspecialchars_decode(); # Predefine html Entity (& " ' < >) To a character
nl2br(); # \n To <br> label
strip_tags(); # Remove... From the string html label
addslashes(); # Specify predefined characters (<>'"&) Add backslash before
stripslashes(); # Delete from addslashes() Added backslash
addcslashes($string, $char); # stay $string In a string , stay $char Add a backslash before the specified character of
stripcslashes(); # Delete from addcslashes() Added backslash
quotemeta(); # Predefined characters in the string (.\+*?[]^$()) Add backslash before
chr(); # Back to different ASCII Value character
ord(); # Returns the of the first character in a string ASCII value
Four 、 File functions
# Open a file or URL, See the manual for more : https://www.php.net/manual/en/function.fopen fopen($filename, $mode, $include_path, $context);$mode Is access type , The following are possible values
“r” ( Read only open , Point the file pointer to the file header )
“r+” ( Read write mode on , Point the file pointer to the file header )
“w” ( Write mode on , Clear the contents of the file , If the file does not exist, try to create it )
“w+” ( Read write mode on , Clear the contents of the file , If the file does not exist, try to create it )
“a” ( Write mode on , Write the file pointer to the end of the file , If the file does not exist, try to create it )
“a+” ( Read write mode on , Save the contents of the file by writing the file pointer to the end of the file )
“x” ( Create a new file and open it in write mode , Returns... If the file already exists FALSE And a mistake )
“x+” ( Create a new file and open it read-write , Returns... If the file already exists FALSE And a mistake )
fclose(); # Close open file pointer
# File attribute
clearstatcache(); # Eliminate the cache of running results
file_exists(); # Check if the file or directory exists
filesize(); # Get file size
filectime(); # Get file creation time
filemtime(); # Get file modification time
fileatime(); # Get the last access time of the file
is_readable(); # Check that the file is readable
is_writable(); # Check that the file is writable
is_executable(); # Check if the file is executable
is_file(); # Check whether the file is a regular file
stat(); # Get most of the information in the file See the manual for details : https://www.php.net/manual/en/function.stat
# File operations
file_get_contents(); # Read the entire file into a string
file_put_contents($file, $data, $mode); # Write a string to a file
fwrite($filename, $string, $length); # take $string write file , $length Is the maximum number of bytes written
fread($filename, $length); # Read open files , $length Is the maximum number of bytes read
feof(); # Check whether the file pointer is the end position
fgets(); # Read a line from the pointer of the file and return
fgetc(); # Reading a single character from the pointer of the file returns
file(); # Read the contents of the file into an array
readfile(); # Read a file , Write the contents of the file to the output buffer .
ftell(); # Returns the current position of the file pointer
fseek($file, $offset); # Locate the file pointer to a certain location
rewind(); # Point the file pointer to the beginning of the file
flock($file, $lock); # Lock or release files
# File upload download
is_uploaded_file(); # Judge whether the document is passed HTTP POST Upload
move_uploaded_file(); # Move the uploaded file to a new location
5、 ... and 、 Directory functions
basename($path, $suffix); # Return the filename part of the path , If specified $suffix , The extension... Is not displayed
dirname(); # Return the directory part of the path
pathinfo(); # Return the file path information
mkdir(); # new directory
rmdir(); # Delete directory
unlink(); # Delete file
copy($file, $to_file); # Copy files
rename($oldname, $newname); # Rename a file or directory
opendir(); # Open directory handle
readdir(); # Read entries from the directory handle
closedir(); # Close directory handle
rewinddir(); # Rewind directory handle
6、 ... and 、 Time function
date_default_timezone_set("Asia/Shanghai"); # Set the default time zone , Time zone table link : https://www.php.net/manual/en/timezones.php
date_default_timezone_get(); # Get the default time value
time(); # Returns the current Unix Time stamp
microtime(); # Returns the current Unix The microseconds of the timestamp
date($format, $timestamp); # Format a local time / date example : date("Y-m-d H:i:s", time());
getdate($timestamp); # Returns information about the time and date of the timestamp , The format is associative array
strtotime(); # Parse string to Unix Time stamp
# strtotime Case study
strtotime('now');
strtotime('+1 day');
strtotime('+1 week 2 days 3 hours 4 seconds');
strtotime('next Monday');
strtotime('last Monday');
边栏推荐
- How to open the port number of the server, and the corresponding port of common network services
- What is the component customization event we are talking about?
- gnuplot软件学习笔记
- SDUT compilation principle experimental code
- Makefile variables and dynamic library static library
- js定位大全获取节点的兄弟,父级,子级元素含robot实例
- TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
- How do tiktok merchants bind the accounts of talents?
- 配置系统环境变量的时候误删了Path怎么办?
- Six pictures show you why TCP shakes three times?
猜你喜欢

Un7.22: how to upload videos and pictures simultaneously with the ruoyi framework in idea and vs Code?

What is the component customization event we are talking about?

Re6:读论文 LeSICiN: A Heterogeneous Graph-based Approach for Automatic Legal Statute Identification fro

Code random notes_ Linked list_ Turn over the linked list in groups of 25K

Common evaluation indexes of medical image segmentation

Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng

The difference between & &, | and |

js定位大全获取节点的兄弟,父级,子级元素含robot实例

Vscode failed to use SSH Remote Connection (and a collection of other problems)

C#/VB. Net: convert word or EXCEL documents to text
随机推荐
The difference between classification and regression
来阿里一年后我迎来了第一次工作变动....
Six pictures show you why TCP shakes three times?
What are the 6% annualized products?
Xtrabackup realizes full backup and incremental backup of MySQL
What is the component customization event we are talking about?
Common evaluation indexes of medical image segmentation
Makefile变量及动态库静态库
How should tiktok shop cooperate with live broadcast in the background?
链表——19. 删除链表的倒数第 N 个结点
Promise basic summary
Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform
【笔记】什么是内核/用户空间 从CPU如何运行程序讲起
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
Foreign lead operation takes one month to collect money, and the sideline still needs it
DSP development, using CCS software to establish engineering and burning
Asyncdata cross domain error after nuxt route switching
Es search summary
One year after I came to Ali, I ushered in my first job change
Will your NFT disappear? Dfinity provides the best solution for NFT storage