7 little-known but super-useful PHP functions

Source: Internet
Author: User

PHP has many built-in functions, most of which are widely used by programmers. But there are some functions hidden in the corner, this article will introduce you to 7 little-known, but very useful functions. A programmer who doesn't use it may wish to come and see.

1.highlight_string ()

The highlight_string () function becomes very useful when you need to present PHP code in a Web site. The function outputs or returns a syntax-highlighting version of a given PHP code by using the color defined in the PHP syntax highlighting program.

 

Example:

1 2 3 <?phphighlight_string(‘<?php phpinfo(); ?>‘);?>

2.str_word_count ()

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

1 2 3 4 <?php$str = "How many words do I have?";echo str_word_count($str); //Outputs 6?>

3.levenshtein ()

The function mainly returns the Levenshtein distance between two strings. Levenshtein distance, also known as the editing distance, refers to the minimum number of edit operations required between two strings, converted from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character. This function is useful for finding typos submitted by users.

Example:

1 2 3 4 5 <?php $str 1  = "Carrot" $str 2  = "Carrrott" echo  levenshtein ( $str 1 $str 2 ); //outputs 2 ?>

4.get_defined_vars ()

The function returns a multidimensional array that contains a list of all defined variables, including environment variables, server variables, and user-defined variables.

Example:

1 print_r(get_defined_vars());

5.escapeshellcmd ()

This function avoids the special symbols in the string, which prevents the user from playing tricks to crack the server system. You can use this function with EXEC () or system () two functions, which can reduce malicious behavior of online users.

Example:

1 2 3 4 5 <?php $command  = './configure ' Code class= "PHP plain". $_post [ ' configure_options ' ]; $escaped _command  = escapeshellcmd ( $command system ( $escaped _command ?>

6.checkdate ()

This function can be used to check if a date is valid, for example, 0-32,767 years, 1-12 months, and days with month and leap year.

Example:

 
1 2 3 4 5 6 7 <?phpvar_dump(checkdate(12, 31, 2000));var_dump(checkdate(2, 29, 2001));//Output//bool(true)//bool(false)?>

7.php_strip_whitespace ()

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

Example:

1 2 3 4 5 6 7 8 9 <?php// PHP comment here/* * Another PHP comment */echophp_strip_whitespace(__FILE__);// Newlines are considered whitespace, and are removed too:do_nothing();?>

Output Result:

1 2 <?php echophp_strip_whitespace(__FILE__); do_nothing(); ?>

7 little-known but super-useful PHP functions

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.