Share 10 useful functions in PHP

Source: Internet
Author: User
PHP is more powerful and has a very rich built-in function. Experienced PHP programmers may be familiar with them, but many of them participate in the PHP training

PHP beginners, are still unfamiliar with some very useful functions. In this article, we list 10 PHP functions that you may not understand but are useful for reference and learning.
1. Php_check_syntax

This function can be used to check if the PHP syntax is correct in a particular file.
How to use:

$error _message = "";

$filename = "./php_script.php";

if (!php_check_syntax ($filename,& $error _message)) {

echo "Errors were found in the file $filename: $error _message";

} else {

echo "The file $filename contained no syntax errors";

}

?>
2. highlight_string

The highlight_string () function is useful when you want to display PHP code on a page, highlighting the PHP code you provide with the built-in syntax highlighting color. This function has two parameters, and the first parameter is the string to be highlighted. The second parameter, if set to true, returns the highlighted code.
How to use:
Highlight_string (' ');

?>
3. Show_source
This function is similar to Highlight_file (), it can display the PHP syntax highlighted file, and is based on the HTML tag syntax highlighting.
How to use:
Show_source ("php_script.php");
?>
4. Php_strip_whitespace
This function is similar to the Show_source () function above, but it deletes comments and whitespace in the file.
How to use:
Echophp_strip_whitespace ("php_script.php");
?>
5. _halt_compiler
It can abort the execution of the compiler, which is useful for embedding data in PHP scripts, just like installing files.
How to use:
$fp = fopen (__file__, ' R ');
Fseek ($fp, __compiler_halt_offset__);
Var_dump (Stream_get_contents ($fp));
The end of the script execution
__halt_compiler ();
?>
6. Highlight_file
This is a very useful php function, it can return the specified PHP file, and follow the syntax highlighting the contents of the file.
How to use:
Highlight_file ("php_script.php");
?>
7. Ignore_user_abort
Using this function, the user can deny the browser end the request to execute the script. Normally, the client's exit causes the server-side script to stop running.
How to use:
Ignore_user_abort ();
?>
8. Str_word_count
This function can be used to count the number of words in a string.
How to use:
Echo Str_word_count ("Hello how areyou!");
?>
9. Get_defined_vars
This function is important when debugging code, and it returns a multidimensional array that includes all of the defined variables.
How to use:
Print_r (Get_defined_vars ());
?>
Ten. Get_browser
This function checks and reads the Browscap.ini file, returning browser-compatible information.
How to use:
echo $_server[' http_user_agent '];
$browser = Get_browser ();
Print_r ($browser);
?>
  • 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.