Home Backend Development PHP Tutorial You must know the commonly used built-in functions in PHP

You must know the commonly used built-in functions in PHP

Jun 28, 2017 am 10:15 AM
php built-in functions

//================================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 The number of the month, D the 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. An error is 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, it will prompt

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

//sprintf("%d","3.2");//Formatting only, return the formatted string, No output.

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

//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

//================================ Common characters String function================================

//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 , concatenate each element in the following array and return a string.

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

//String splitting method, returns an array, using the The characters in one parameter are used to split the following string, and the characters before, after, and between the specified characters are intercepted. If the specified character is at the beginning or end, the element at the beginning or end of the returned array will be 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);

//Eliminate the left side of the string spaces at the beginning, and returns

//If there is a second parameter, the spaces at the beginning on the left are removed and the string in the second parameter is removed

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

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

$str=rtrim(" asd ");

//Remove the first Strings starting with the second parameter on both sides of a string are eliminated. If there is no second parameter, the spaces at the beginning of both sides of the string will be removed by default

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

//From the first string The length (number) of characters starting from the specified position in the parameter. The first character position in the string is calculated from 0.

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

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

//Change the third parameter The first parameter string is replaced with the second parameter string

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

//The usage is the same as str_replace, except Case-insensitive

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

//Return the characters of the string in brackets All uppercase string

$str=strtoupper("sdaf");

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

$str =ucfirst("asdf");

//Use echo 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 characters

$ str=htmlentities("
");

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

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

//Return 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");

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

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

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

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

//Interception returns the last parameter two from left to right in parameter one to the last parameter one Character string

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

//Add "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 (the number of single characters ), parameter three is the specified padding string, do not write the default space

//Parameter four padding position, 0 is padded at the beginning of the left side of parameter one, 1 is padded at the right side, and 2 is padded at the beginning of both sides. If not written, it will be padded at the beginning on the right side 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");

//Return the formatted number format of the first parameter, and the second parameter is to retain a few decimal places, Parameter three is to replace the decimal point with parameter three. Parameter four is what characters are used to separate every three digits in the integer part.

//If the last three parameters are not written, the decimal part will be removed by default, and the integers will be separated every three Separate the three digits with commas. 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 the array element of the second parameter There is the first parameter element

$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);

//Return a new array composed of all element values ​​​​in the original array. The key values ​​​​increment 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 key value and element value of the element pointed to by the current array pointer, Then push the pointer to the next bit, and finally the pointer points to an empty element and returns empty

//There are four fixed key values ​​in the returned array. The element values ​​corresponding to the return element are the key value and the element. Value, where 0, 'key' key value corresponds to the returned element key value, 1, 'value' key value corresponds to the returned element value

$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 up One bit, and then return the element value pointed to after the pointer moves

$value=prev($arr);

//Let the array pointer reset 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 starts from the smallest unused value, and returns the subsequent array length

$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 original element remains unchanged, and the array length after return 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, and the index will start from the smallest unused value and return , the original array does not change

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

//Return a new array with the redundant duplicate elements in the original array removed. The original array The array does not change

$array=array_unique($array1);

//Break the original array key value and re-sort the Asker code value of the element value from small to large, and the index is from the number 0 Start recalculating

$int=sort($array);

//In contrast to sort, re-sort in descending order of the element value Asko code value, and re-calculate 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 Consistent, 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 default. 1

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

//Rearrange the corresponding relationship between the original array elements and the corresponding key values ​​randomly and return true and 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. True is retained, and the default is false.

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

//json_encode() converts the array into JSON format string return

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

echo $encode_str = json_encode($arr);

//json_decode() converts a JSON format string into an object that can be coerced into an array and returns, JSON format characters Double quotes must be used when the keys and values ​​in the string need to be enclosed in quotes

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

var_dump($decode_arr);

The above is the detailed content of You must know the commonly used built-in functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
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 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,

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.

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

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.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles