Home >Backend Development >PHP Tutorial >Share 10 practical functions in PHP! (with code)

Share 10 practical functions in PHP! (with code)

藏色散人
藏色散人Original
2018-09-29 17:20:513113browse

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 participate in PHP training are still not familiar with some very useful functions. Here we list 10 PHP functions that you may not know but are practical. At the same time, the PHP Chinese website also provides a wealth of PHP Class Library resources for everyone to download for reference and study.

1.php_check_syntax

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

<?php
$error_message = "";
$filename = "./php_script.php";
if(!php_check_syntax($filename, &$error_message)) {
   echo "Errors were found in the file $filename: $error_
} else {
   echo "The file $filename contained no syntax errors";
}
?>

2. highlight_string
The highlight_string() function is very useful when you want to display PHP code on the page. It can be defined using the built-in Syntax highlighting colors highlight the PHP code you provide. 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:

<?php
highlight_string(&#39; <?php phpinfo(); ?>&#39;);
?>

3. show_source
The operation of this function is similar to highlight_file(). It can display the PHP syntax highlighted file, and Syntax highlighting is performed based on HTML tags.
Usage:

<?php
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:

<?php
echo php_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. Just like the installation files.
Usage:

<?php
$fp = fopen(__FILE__, &#39;r&#39;);
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 it according to the syntax Highlight file contents.
Usage:

<?php
highlight_file("php_script.php");
?>

7. ignore_user_abort
Using this function, the user can refuse the request of the browser to terminate the execution of the script. Under normal circumstances, the client's exit will cause the server-side script to stop running.
Usage:

<?php
ignore_user_abort();
?>

8. str_word_count
This function can be used to count the number of words in a string.
Usage:

<?php
echo str_word_count("Hello How Are You!");
?>

9. get_defined_vars
This function is very important when debugging code. It will return a multi-dimensional array including all defined variables. .
Usage:

<?php
print_r(get_defined_vars());
?>

10. get_browser
This function checks and reads the browscap.ini file and returns browser compatibility information.
How to use:

<?php
echo $_SERVER[&#39;HTTP_USER_AGENT&#39;];
$browser = get_browser();
print_r($browser);
?>

If you want to know more about PHP, you can follow the PHP Chinese website PHP video tutorial, welcome Please refer to and learn from everyone!

The above is the detailed content of Share 10 practical functions in PHP! (with code). For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more