Home  >  Article  >  Backend Development  >  Introducing 10 practical functions in PHP

Introducing 10 practical functions in PHP

韦小宝
韦小宝Original
2017-11-30 10:10:383847browse

PHP is becoming more and more powerful, and it has a very rich built-in function. Senior PHP programmers may be familiar with them, but many PHP learners are still not familiar with some very useful functions. In this article, we list 10 PHP functions that you may not know but are practical for your reference and learning. Okay, without further ado, let’s get started!

1. php_check_syntax

This function can be used to check PHP in a specific file Is the syntax correct?

Usage:

<?php
$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 The highlight_string() function is very useful when displaying PHP code on the page. 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:

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

##3 . show_source

This function operates similarly to highlight_file(). It can display the PHP syntax highlighted file and is based on

HTML tag is used for syntax highlighting.

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 abort the execution of the compiler, which is helpful for embedding data in PHP scripts, like 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, which can return the specified PHP file and highlight the file content according to syntax highlighting.

Usage:

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

7. ignore_user_abort

Using this function, the user can reject 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:

<?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 useful when debugging the code. Important, it will return a

multidimensional 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 to browsing device compatibility information.

Usage:

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

The above are 10 practical functions in PHP. I hope it will be of great help to the students. For more, please visit this site search!

Related recommendations:
The basic structure of PHP functions

PHP function trim() example

The above is the detailed content of Introducing 10 practical functions in PHP. 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