search
HomeBackend DevelopmentPHP TutorialYou 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
phpbuilt-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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.