PHP function learning comments

Source: Internet
Author: User
PHP function learning-PHP function comments 1. print_r ()
Prints easy-to-understand information about Variables. if it is an array, the structure of the array is displayed.
For example:

The code is as follows:


$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => array ('X', 'y ', 'Z '));
Print_r ($ );
?>


Axgle comment: Viewing the structure information of any array is a necessary tool for program debugging. If any returned result is an array "function", you only need print_r to check the details at a glance!
2. var_export ()
Outputs or returns a string representation of a variable.
This function returns the structure information about the variables passed to this function. it is similar to print_r (). The difference is that the returned representation is legal PHP code.
You can set the second parameter of the function to TRUE to return the expression of the variable.
For example:

The code is as follows:


$ A = array (1, 2, array ("a", "B", "c "));
Var_export ($ );
Echo "";
$ V = var_export ($ a, TRUE );
Echo $ v;
?>


Axgle comment: In the above example, $ v = var_export ($ a, TRUE) returns the php code ~~ You can save it as a php file.
What is saved as a php file? This can be used as a "cache". you can include it directly when necessary.
3. file ()
File () returns the object as an array. Each element in the array is a corresponding line in the file, including line breaks. If file () fails, FALSE is returned.

The code is as follows:


// Read a file into an array.
$ Lines = file('test.txt ');
// View the structure of this array
Print_r ($ lines );
?>


Axgle comment: the file () function is a function that surprised me when I first came into contact with php. In C and vb
I felt that there was no more convenient file read/write method than the file () function.
4. phpinfo ()
Print php-related information, such as the PHP version, function support, and global variables.
For example:
Phpinfo ();
?>
Axgle comment: a simple function that keeps you updated on 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)
The function of reading the entire file into a string. file_get_contents () is the preferred method for reading the file content into a string. If the operating system supports it, the memory ing technology will be used to enhance the performance.
For example:
$ Data = file_get_contents('test.txt ');
Echo $ data;
?>
6. file_put_contents (note: PHP 5)
Write a string directly to the file.
For example:
// Address of an image
$ Url = "http://...test.com/plmm.jpg ";
// Read the binary "string"
$ Data = file_get_contents ($ url );
// Save it to your computer
File_put_contents (" .jpg", $ data );
?>
Axgle comment: If you find that the image name of a beauty Image website is as follows: 1.JPG, 2.jpg...
OK, use a for loop to capture all the "beautiful women". Don't be so excited that your girlfriend
Jealous ~~~
7. function_exists
Returns true if the function exists.
For example:
// If the function does not exist, you can 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
Returns an array to obtain all defined php functions.
For example:

The code is as follows:


$ Arr = get_defined_functions ();
Print_r ($ arr );
?>


Axgle Comment: Now you know all the function names. If you want to know the use of a function, you can use the form such as http://www.php.net/function_name online query, "Bao cure all diseases, all kinds of difficult diagnosis, medicine to remove disease ~~~~"
9. get_declared_classes
Returns an array to obtain all defined php classes.
For example:

The code is as follows:


$ Arr = get_declared_classes ();
Print_r ($ arr );
?>


Axgle comment: you can see this function after Example 8 is run. Run this function in php4 to obtain only a few classes. However, if you use php5, you will see dozens of predefined php classes in this example! It can be seen that php5 is much more object-oriented.
10. exit
Output the message and stop the current script. (Note: like echo, this is not a "function", but a "statement ").
For example:
Echo "statement 1 ";
Exit ("the following statement 2 does not output ");
Echo "statement 2 ";
?>
Axgle comments: it is useful to debug the program and find the location of the error.
There are many useful PHP functions, and some very interesting PHP functions can be shared. I will introduce them later.

The above is the PHP function comments for PHP function learning. for more related articles, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.