Table of Contents
Collection of some common functions in PHP
Home Backend Development PHP Tutorial Collection of some common functions in PHP_PHP tutorial

Collection of some common functions in PHP_PHP tutorial

Jul 13, 2016 am 09:52 AM
php main function Commonly used collect article time

Collection of some common functions in PHP

This article mainly introduces the collection of some common functions in PHP. This article collects some time and date, output printing, and commonly used string functions. , commonly used array methods, friends in need can refer to it

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

//================================Time and date============ ===================

//y returns the last two digits of the year, the four-digit number of year Y, the number of month m, and the English month of M. d number of the month, D day of the week in English

$date=date("Y-m-d");

$date=date("Y-m-d H:i:s");//with hours, minutes and seconds

//include,include_once.require,require_once

//require("file.php") Before the PHP program is executed, the file specified by require will be read in. If an error occurs, it will be fatal.

//include("file.php") can be placed anywhere in the PHP program. The file specified by include will not be read until the PHP program is executed. If an error occurs,

will be prompted.

//================================Output printing============ ===================

//sprintf("%d","3.2") ;//Only formatting, returns the formatted string, no output.

//printf("%d","3.2") ;//Formatting and outputting

//print("3.2") ;//Only output

//echo "nihao","aa";//Can output multiple strings

//print_r(array("a","b","c"));//Display the key values ​​and elements of the array in sequence

//============================== Commonly used string functions========== =====================

//Get the length of the string, how many characters there are, spaces are also counted

$str=" sdaf sd ";

$len=strlen($str);

//Use the string in the first parameter to connect each element in the subsequent array and return a string.

$str=implode("-",array("a","b","c"));

//String splitting method, returns an array, uses the characters in the first parameter to split the following string, intercepts before, after, and between the specified characters. If the specified character is at the beginning or end, the beginning of the array returned Or the ending element is an empty string

//If it is not divided into strings, a null value will be returned to the corresponding element of the array. The last limit returns the length of the array. If there is no limit, it will continue to be divided.

$array=explode("a","asddad addsadassd dasdadfsdfasdaaa",4);

//print_r($array);

//Remove the leading spaces on the left side of the string and return

//If there is a second parameter, the leading spaces on the left will be removed instead of the string in the second parameter

$str=ltrim("a asd ","a");

//Remove spaces at the beginning of the right side of the string

$str=rtrim(" asd ");

//Remove the strings starting with the second parameter on both sides of the first string. If there is no second parameter, the leading spaces on both sides of the string will be removed by default

$str=trim(" sdsdfas ","a");

//How long (how many) characters are taken starting from the specified position in the first parameter of the string, and the first character position in the string is calculated from 0.

//If the second parameter is negative, the length of the string will be taken starting from the last number at the end of the string. The last character at the end counts -1, and the interception direction is always from left to right

$str=substr("abcdefgh",0,4);

//Replace the first parameter string of the third parameter with the second parameter string

$str=str_replace("a","","abcabcAbca");

//Same usage as str_replace, except case-insensitive

//$str=str_ireplace("a"," ","abcabcAbca");

//Returns a string in which the characters in the string in brackets are all uppercase

$str=strtoupper("sdaf");

//Change the first string in the brackets to uppercase and return

$str=ucfirst("asdf");

//Use echo, etc. to print the string in the brackets on the web page, and the string in the brackets will be printed out as it is, including the label character

$str=htmlentities("
");

//Return the number of times the second parameter string appears in the first string

$int=substr_count("abcdeabcdeablkabd","ab");

//Returns the position where the second string appears for the first time in the first string. The first character position is counted as 0

$int=strpos("asagaab","ab");

//Returns the position where the second string last appears in the first string, and the first character position is counted as 0

$int=strrpos("asagaabadfab","ab");

//Intercept and return the string string from the first occurrence of parameter two to the last character of parameter one from left to right in parameter one

$str=strstr("sdafsdgaababdsfgs","ab");

//Intercept and return the string string from the last occurrence of parameter two to the last character of parameter one from left to right in parameter one

$str=strrchr("sdafsdgaababdsfgs","ab");

//Add ""

before each character in parameter two before the same character in parameter one

$str=addcslashes("abcdefghijklmn","akd");

// Fill the string of parameter one to the length specified by parameter two (number of single characters). Parameter three is the specified filled string, do not write the default space

//Parameter four filling position, 0 is filled at the beginning of the left side of parameter one, 1 is filled at the beginning of the right side, and 2 is filled at the beginning of both sides. If not written,

will be padded at the beginning on the right by default.

$str=str_pad("abcdefgh",10,"at",0);

//Compare the Asker code values ​​of the corresponding characters in the two strings in turn. If the first pair is different, if the first pair is greater than the second parameter, 1 will be returned. Otherwise, -1 will be returned. If the two strings are exactly the same, 0 will be returned.

$int1=strcmp("b","a");

//Returns the formatted number format of the first parameter. The second parameter is to retain a few decimal places. The third parameter is to replace the decimal point with parameter three. The fourth parameter is what is used for each three digits of the integer part. Character segmentation

//If the last three parameters are not written, the decimal part will be removed by default, and the integer will be separated by commas every three digits. Parameter three and parameter four must exist at the same time

$str=number_format(1231233.1415,2,"d","a");

//============================== Commonly used array methods =========== ====================

$arr=array("k0"=>"a","k1"=>"b","k2"=>"c");

//Return the number of array elements

$int=count($arr);

//Determine whether there is a first parameter element in the array element of the second parameter

$bool=in_array("b",$arr);

//Returns a new array composed of all the key values ​​​​of the array in brackets. The original array does not change

$array=array_keys($arr);

//Determine whether the array of the second parameter contains the key value of the first parameter and return true or false

$bool=array_key_exists("k1",$arr);

//Returns a new array composed of all element values ​​in the original array. The key values ​​increase from 0 and the original array remains unchanged

$array=array_values($arr);

//Return the key value pointed to by the current array pointer

$key=key($arr);

//Return the element value pointed to by the current array pointer

$value=current($arr);

//Return the array consisting of the key value and element value of the element pointed by the current array pointer, and then push the pointer to the next position. Finally, the pointer points to an empty element and return empty

//There are four element values ​​corresponding to fixed key values ​​in the returned array, which are the key value and element value of the returned element, among which 0, 'key' key value corresponds to the returned element key value, 1, 'value' The key values ​​correspond to the returned element values ​​

$array=each($arr);

//First push the array pointer to the next bit, and then return the element value pointed to after the pointer moves

$value=next($arr);

// Push the array pointer to the previous position, and then return the element value pointed to after the pointer moves

$value=prev($arr);

//Reset the array pointer to point to the first element and return the element value

$value=reset($arr);

//Point the array pointer to the last element and return the last element value

$value=end($arr);

//Append the parameters after the first parameter as elements to the end of the first parameter array, the index is calculated from the smallest unused value, and the subsequent array length is returned

$int=array_push($arr,"d","dfsd");

//Add all parameters after the first parameter array as elements to the beginning of the first parameter array. The key value is re-accumulated from the first element with 0. The original non-numeric key value remains unchanged. The sorting position of the elements remains unchanged, and the array length after returning is

$int=array_unshift($arr,"t1","t2");

//Return to extract the last element value from the end of the array, and remove the last element from the original array

$value=array_pop($arr);

//array_pop On the contrary, extract and return the first element value of the array, and remove the first element from the original array

$value=array_shift($arr);

//Let the first parameter array reach the length of the second parameter value, add the third parameter as an element to the end of the first parameter array, the index is calculated from the smallest unused value and returned, the original array No change

$array1=array_pad($arr,10,"t10");

//Returns a new array with excess duplicate elements removed from the original array, and the original array remains unchanged

$array=array_unique($array1);

//Break the original array key values ​​and sort them by the Asker code value of the element values ​​from small to large, and the index will be recalculated from the number 0

$int=sort($array);

//Contrary to sort, reorder the element value in descending order of Asko code value, and recalculate the index from 0

$int=rsort($array);

//Returns an array in which each element value in the first parameter array is paid as a key value to the second parameter array in turn. The length of the two arrays must be the same, and the original array does not change

$array=array_combine(array("a","b","c","d","e"),$arr);

//Merge the two arrays and return the original array unchanged

$array=array_merge($arr,array("a","b","c"));

//In the first parameter array, intercept the array key value element starting from the second parameter value position to the third parameter value length and return it. The first element position of the array is counted from 0

$array=array_slice($arr,2,1);

//The interception function is the same as array_slice(), except that the intercepted part is removed from the original array

$array=array_splice($arr,2,1);

// Take the first parameter as the first element, increment the value of parameter three each time, and then store it in the array as an element after incrementing until the value reaches the value of parameter two and save it in the array. and return this array

//Parameter one, parameter two can be a number or a single character. A single character is calculated according to the ASCO code value. If the third parameter is not written, it will increment by 1 each time by default

$array=range(3,9,2);

//Rearrange the correspondence between the original array elements and the corresponding key values ​​randomly and return true or false

$bool=shuffle($arr);

//Calculate the sum of all numeric element values ​​in the array

$int=array_sum(array("a",2,"cssf"));

// Split an array into new array blocks. Each element of the new array is an array. The number of elements in each element of the new array is determined by parameter two

//The third parameter determines whether the key value of the element retains the original key value and does not need to be written. true means retaining, and the default is false not retaining

$array=array_chunk(array("a"=>"a","b","c","d","e","f","g","h"),2 ,true);

//json_encode() converts the array into a JSON format string and returns

$arr = array('k1'=>'val1','k2'=>'val2','k3'=>array('v3','v4'));

echo $encode_str = json_encode($arr);

//json_decode() converts the JSON format string into an object that can be coerced into an array and returns it. When the keys and values ​​in the JSON format string need to be enclosed in quotes, double quotes must be used

$decode_arr = (array)json_decode($encode_str);

var_dump($decode_arr);

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1006578.htmlTechArticleCollection of some common functions in PHP This article mainly introduces the collection of some common functions in PHP. This article collects Some time and date, output printing, common string functions, common arrays...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles