SoFunction
Updated on 2025-04-09

PHP function learning reviews

1.print_r()
Print easy-to-understand information about variables. If it is an array, the structure information of the array is displayed.
For example:
Copy the codeThe code is as follows:

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>

axgle comment: Viewing the structure information of any array is a must-have tool for program debugging. For any "function" that returns the result as an array, just print_r and everything is clear at a glance!
2.var_export()
Output or return a string representation of a variable
This function returns structural information about the variable passed to the function. It is similar to print_r(), except that the representation it returns is legal PHP code.
You can return a representation of the variable by setting the second parameter of the function to TRUE.
For example:
Copy the codeThe code is as follows:

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export ($a);
echo "<hr>";
$v = var_export($a, TRUE);
echo $v;
?>

axgle comment: In the above example, $v = var_export($a, TRUE) returns the php code ~~ Then you can save it as a php file.
What to do if you save as a php file? Haha, this can be used as a "cache", and when needed, you can include it directly.
()
file() returns the file as an array. Each element in the array is a corresponding line in the file, including line breaks. If failed file() returns FALSE.
Copy the codeThe code is as follows:

<?php
// Read a file into an array.
$lines = file('');
// Check the structure of this array
print_r($lines);
?>

axgle comment: The file() function is a function that surprised me very much in the early days of my exposure to php. Compared to the past, I've been using C and VB
The extremely troublesome experience of reading and writing files made me feel that at that time there was no more convenient way to read and write files than the file() function.
()
Print information related to php, such as PHP version, feature support, global variables, etc.
For example:
<?php
phpinfo();
?>
Axgle comment: A simple function allows you to always understand the rapid development of php---If you pay close attention to the development of php~~~~
5.file_get_contents() (Note: PHP 4 >= 4.3.0, PHP 5)
Read the entire file into a string. The file_get_contents() function is the preferred method used to read the contents of a file into a string. Memory mapping technology is also used to enhance performance if the operating system supports it.
For example:
<?php
$data = file_get_contents('');
echo $data;
?>
6. file_put_contents (Note: PHP 5)
Write a string directly to the file.
For example:
<?php
//The address of a certain image
$url="http://.../";
//Read binary "string"
$data=file_get_contents($url);
//Save to your computer
file_put_contents("Beauty.jpg",$data);
?>
axgle comment: If you find that the image name of a beautiful girl website is such as,...
OK, use a for loop to catch all the "beauties" and don't be too excited to get your girlfriend
Jealous ~~~
7.function_exists
If the function exists, return true
For example:
<?php
//If the function does not exist, customize the function
if(!function_exists('file_put_contents')) {
function file_put_contents($filename,$data) {
$fp=fopen($filename,"wb");
fwrite($fp,$data);
fclose($fp);
}
}
?>
8.get_defined_functions
Return an array to get all defined php functions.
For example:
Copy the codeThe code is as follows:

<?php
$arr = get_defined_functions();
print_r($arr);
?>

axgle comment: Now you know all the function names. If you want to know the usage of a certain function, you can use the "Share to cure all diseases, all kinds of difficult and complicated diagnosis, and cure the disease~~~~"
9.get_declared_classes
Return an array to get all defined php classes.
For example:
Copy the codeThe code is as follows:

<?php
$arr = get_declared_classes();
print_r($arr);
?>

axgle comment: I believe that you can see this function after running Example 8. Running this function in php4 can only get a few classes; but if you use php5, then in this example you will see dozens of predefined php classes! It can be seen that php5 has been greatly enhanced in object-oriented.

Output a message and stop the current script. (Note: Like echo, this is not a "function", but a "statement").
For example:
<?php
echo "Sentence 1";
exit("The following statement 2 will not output");
echo "Sentence 2";
?>
axgle comment: Debugging the program, finding the location where the error is found, etc. are more useful.
There are many useful PHP functions, and there are some very interesting PHP functions to share. I will introduce them if I have time.