Some practical PHP built-in function User Guide

Source: Internet
Author: User
This article recommends seven less frequently used php built-in functions, which are very practical and powerful, and save a lot of time.

This article recommends seven less frequently used php built-in functions, which are very practical and powerful, and save a lot of time.

PHP has many built-in functions, most of which are widely used by programmers. But there are also some functions hidden in the corner. This article will introduce seven little-known functions that are very useful. Programmers who have never used it may wish to come and have a look.

1. highlight_string ()

When you need to display PHP code on a website, the highlight_string () function becomes very useful. This function outputs or returns the syntax highlighted version of the given PHP code by highlighting the color defined in the PHP syntax program.

Example:

The Code is as follows:


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

2. str_word_count ()

This function must pass a parameter that returns the number of words based on the parameter type. As shown in the following figure:

The Code is as follows:


<? Php
$ Str = "How many words do I have? ";
Echo str_word_count ($ str); // Outputs 6
?>

3. levenshtein ()

This function mainly returns the Levenshtein distance between two strings. Levenshtein distance, also known as the editing distance, refers to the minimum number of edits required to convert a string from one to another. Licensed editing operations include replacing one character with another, inserting one character, and deleting one character. This function is useful for searching user-submitted typos.

Example:

The Code is as follows:


<? Php
$ Str1 = "carrot ";
$ Str2 = "carrrott ";
Echo levenshtein ($ str1, $ str2); // Outputs 2
?>

4. get_defined_vars ()

This function returns a multidimensional array containing the list of all Defined variables, including environment variables, server variables, and User-Defined variables.

Example:

The Code is as follows:


Print_r (get_defined_vars ());

5. escapeshellcmd ()

This function is used to avoid special characters in the string and prevent users from attracting hackers to crack the server system. This function can be used with exec () or system () functions to reduce the malicious behavior of online users.

Example:

The Code is as follows:


<? Php
$ Command = './configure'. $ _ POST ['configure _ options'];
$ Escaped_command = escapeshellcmd ($ command );
System ($ escaped_command );
?>

6. checkdate ()

This function can be used to check whether the date is valid. For example, the year is 0 to 32767, the month is 1 to December, and the day changes with the month and the leap year.

Example:

The Code is as follows:


<? Php
Var_dump (checkdate (12, 31,200 0 ));
Var_dump (checkdate (2, 29,200 1 ));
// Output
// Bool (true)
// Bool (false)
?>

7. php_strip_whitespace ()

This function can return source code files with deleted PHP comments and blank characters, which is useful for comparing the actual number of codes with the number of comments.

Example:

The Code is as follows:


<? Php
// PHP comment here
/*
* Another PHP comment
*/
Echo php_strip_whitespace (_ FILE __);
// Newlines are considered whitespace, and are removed too:
Do_nothing ();
?>

Output result:

The Code is as follows:


<? Php
Echo php_strip_whitespace (_ FILE _); do_nothing ();?>

How many of the seven built-in php functions have you used? It is estimated that most people have never used these built-in functions. In fact, there are many such built-in functions. Here we will introduce these 7 functions to you, and we will make up other functions later (I will repeat them to make a summary when I make up for them, I really don't know how to use it)

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.