PHP provides a series of built-in functions for performing various tasks, including: string operations (strcmp, strtoupper, strtolower) array processing (array_push, array_pop, in_array) mathematical operations (round, abs, max) file processing ( fopen, fread, fclose)
Introduction to PHP built-in functions
PHP built-in functions are a set of functions predefined in PHP , which provide convenient methods for handling a variety of tasks, such as string manipulation, array manipulation, mathematical operations, and file processing. This article will introduce some commonly used built-in functions and demonstrate their usage through practical cases.
String function
$result = strcmp("Hello", "World"); // 结果为 -1,"Hello" 小于 "World"
$string = "Hello World"; $stringToUpper = strtoupper($string); // 结果为 "HELLO WORLD"
$string = "HELLO WORLD"; $stringToLower = strtolower($string); // 结果为 "hello world"
Array function
$array = [1, 2, 3]; array_push($array, 4); // 结果为 [1, 2, 3, 4]
$array = [1, 2, 3, 4]; $lastElement = array_pop($array); // 结果为 4,数组变为 [1, 2, 3]
$array = [1, 2, 3, 4]; if (in_array(2, $array)) { echo "2 exists in the array"; // 输出 "2 exists in the array" }
Mathematical functions
$number = 3.14159; $roundedNumber = round($number, 2); // 结果为 3.14
$number = -5; $absoluteNumber = abs($number); // 结果为 5
$numbers = [1, 3, 5, 7, 9]; $maxNumber = max($numbers); // 结果为 9
File function
$handle = fopen("myfile.txt", "r"); // 读模式打开 myfile.txt
$data = fread($handle, 100); // 从 myfile.txt 中读取前 100 个字节
fclose($handle); // 关闭 myfile.txt 文件句柄
The above is the detailed content of Introduction to PHP built-in functions. For more information, please follow other related articles on the PHP Chinese website!