10 PHP functions that you may not know but are useful

Source: Internet
Author: User
PHP is becoming more and more powerful, with a rich set of built-in functions. Senior PHP programmers may be familiar with them, but many PHP learners are still not familiar with some very useful functions.

PHP is becoming more and more powerful, with a rich set of built-in functions. 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 will list 10 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";}?>

Reference: http://www.php.net/manual/en/function.phpchecksyntax.php

2. highlight_string

When you want to display the PHP code to the page, the highlight_string () function is very useful. it can highlight the PHP code you provide with the built-in defined syntax highlighted color. This function has two parameters. The first parameter is the string to be highlighted. If the second parameter is set to TRUE, the highlighted code is returned.

Usage:

highlight_string(' ');?>

Reference: http://www.php.net/manual/en/function.highlightstring.php

3. show_source

This function is similar to highlight_file (). It can display files highlighted by PHP syntax and highlight the syntax based on HTML tags.

Usage:

show_source("php_script.php");?>

Reference: http://www.php.net/manual/en/function.showsource.php

4. php_strip_whitespace

This function is similar to the show_source () function, but it deletes comments and space characters in the file.

Usage:

echo php_strip_whitespace("php_script.php");?>

Reference: http://www.php.net/manual/en/function.phpstripwhitespace.php

5. _ halt_compiler

It can abort the execution of the compiler, which is very helpful for embedding data in PHP scripts, just like the installation file.

Usage:

$fp = fopen(__FILE__, 'r');fseek($fp, __COMPILER_HALT_OFFSET__);var_dump(stream_get_contents($fp));// the end of the script execution__halt_compiler();?>

Reference: http://www.php.net/manual/en/function.haltcompiler.php

6. highlight_file

This is a very useful PHP function that can return the specified PHP file and highlight the file content according to the syntax.

Usage:

highlight_file("php_script.php");?>

Reference: http://www.php.net/manual/en/function.highlightfile.php

7. ignore_user_abort

With this function, you can reject the browser's request to terminate the script execution. Under normal circumstances, the exit of the client will cause the script on the server to stop running.

Usage:

ignore_user_abort();?>

Reference: http://www.php.net/manual/en/function.ignoreuserabort.php

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 Are You!");?>

Reference: http://php.net/manual/en/function.strwordcount.php

9. get_defined_vars

This function is very important for code debugging. It returns a multi-dimensional array containing all defined variables.

Usage:

print_r(get_defined_vars());?>

Reference: http://php.net/manual/en/function.getdefinedvars.php

10. get_browser

This function checks and reads the browscap. ini file and returns browser compatibility information.

Usage:

echo $_SERVER['HTTP_USER_AGENT'];$browser = get_browser();print_r($browser);?>

Reference: http://www.php.net/manual/en/function.getbrowser.php

Original article: http://www.phpzag.com/10littleknownbutusefulphpfunctions/

Http://www.php100.com/html/dujia/2015/0128/8500.html.

Translation: PHP100_Zeroing

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.