Home >Backend Development >PHP Tutorial >Share 10 practical functions in PHP

Share 10 practical functions in PHP

巴扎黑
巴扎黑Original
2016-11-07 17:42:35964browse

PHP is becoming more and more powerful, and it has a very rich set of built-in functions. Senior PHP programmers may be familiar with them, but many PHP beginners who have participated in PHP training

are still not familiar with some very useful functions. In this article, we list 10 practical PHP functions that you may not know but are useful for your reference and learning.
1. php_check_syntax

This function can be used to check whether the PHP syntax in a specific file is correct.
Usage:

$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

When you want to put PHP When the code is displayed on the page, the highlight_string() function is very useful. It can highlight the PHP code you provide using the built-in defined syntax highlighting color. This function takes two parameters, the first parameter is the string to be highlighted. If the second parameter is set to TRUE, the highlighted code will be returned.
Usage:
highlight_string(' ');

?>
3. show_source
This function operates similarly to highlight_file(). It can display PHP syntax highlighted files and is based on HTML Tags are syntax highlighted.
Usage:
show_source("php_script.php");
?>
4. php_strip_whitespace
This function is similar to the show_source() function above, but it will delete comments and spaces in the file.
Usage:
echophp_strip_whitespace("php_script.php");
?>
5. _halt_compiler
It can halt the execution of the compiler, which is helpful for embedding data in PHP scripts, like The installation files are the same.
Usage:
$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 that returns the specified PHP file and highlights the file content according to syntax highlighting.
Usage:
highlight_file("php_script.php");
?>
7. ignore_user_abort
Using this function, the user can refuse the browser's request to terminate the execution of the script. Under normal circumstances, the client's exit will cause the server-side script to stop running.
Usage:
ignore_user_abort();
?>
8. str_word_count
This function can be used to count the number of words in a string.
Usage:
echo str_word_count("Hello How AreYou!");
?>
9. get_defined_vars
This function is very important when debugging code, it will return a multi-dimensional array including all defined variables .
Usage:
print_r(get_defined_vars());
?>
10. get_browser
This function checks and reads the browsercap.ini file and returns browser compatibility information.
Usage:
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>

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