当前位置:网站首页>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');
边栏推荐
- Scarcity in Web3: how to become a winner in a decentralized world
- [don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking
- Vector control of permanent magnet synchronous motor (I) -- mathematical model
- 2021 robocom world robot developer competition - undergraduate group (Preliminary) problem solution
- [note] what is kernel / user space? Let's start with how the CPU runs the program
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
- 详解LinkedList
- Getting started with web security - open source firewall pfsense installation configuration
- Makefile变量及动态库静态库
- We were tossed all night by a Kong performance bug
猜你喜欢

Android系统安全 — 5.2-APK V1签名介绍

Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro

Racecar multi-point navigation experiment based on ROS communication mechanism

Leetcode94 detailed explanation of middle order traversal of binary tree

dp最长公共子序列详细版本(LCS)
![[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?](/img/84/48a6a83192a12dafd88bcd74db0955.gif)
[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?

排序入门—插入排序和希尔排序
![[the first anniversary of my creation] love needs to be commemorated, so does creation](/img/89/2f8eec4f0a0bcf77d5a91179012899.png)
[the first anniversary of my creation] love needs to be commemorated, so does creation

云原生(十二) | Kubernetes篇之Kubernetes基础入门

Account 1-3
随机推荐
A null pointer exception is reported when the wrapper class inserts into the empty field of the database table
03_ UE4 advanced_ illumination
js定位大全获取节点的兄弟,父级,子级元素含robot实例
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Leetcode question brushing series -- 174. Dungeon games
[Luogu p5829] [template] mismatch tree (string) (KMP)
华为无线设备安全策略配置命令
OPENCV学习DAY5
[don't bother to strengthen learning] video notes (II) 2. Write a small example of Q learning
Ue5 film and television animation rendering MRQ layered learning notes
Opencv learning Day5
gnuplot软件学习笔记
【汇编语言实战】(二)、编写一程序计算表达式w=v-(x+y+z-51)的值(含代码、过程截图)
Detailed sequence traversal of leetcode102 binary tree
Read write lock, shared lock, exclusive lock
唐宇迪opencv-背景建模
Es document CRUD
Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)
Understanding of magnetic parameters in Hall sensors
Leetcode102-二叉树的层序遍历详解