Share 10 useful functions in PHP

Source: Internet
Author: User
Tags php script

Share the ten useful functions in PHP

PHP is more powerful and has a very rich built-in function. Experienced PHP programmers may be familiar with them, but many PHP beginners who are involved in PHP training are still unfamiliar with some very useful functions. In this article, we enumerate the 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 the specific file PHP syntax is correct.

How to use:

<?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 to put Thehighlight_string () function is useful when the PHP code is displayed on the page, and it can highlight 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:

<?php

Highlight_string (' <?php phpinfo ();?> ');

?>

3. Show_source

This function is similar to Highlight_file () , it can display The file after the PHP syntax is highlighted, and is based on the HTML tags are highlighted in syntax.

How to use:

<?php

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:

<?php

Echo php_strip_whitespace ("php_script.php");

?>

5. _halt_compiler

It can abort the execution of the compiler, which embedding data in a PHP script is helpful, just like installing files.

How to use:

<?php

$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 contents of the file according to syntax highlighting.

How to use:

<?php

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:

<?php

Ignore_user_abort ();

?>

8. Str_word_count

This function can be used to count the number of words in a string.

How to use:

<?php

Echo Str_word_count ("Hello How is you!");

?>

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:

<?php

Print_r (Get_defined_vars ());

?>

Ten. Get_browser

This function checks and reads the Browscap.ini file, returning browser-compatible information.

How to use:

<?php

echo $_server[' http_user_agent '];

$browser = Get_browser ();

Print_r ($browser);

?>

Share 10 useful functions in PHP

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.