Seven little-known but super-practical PHP functions and little-known php Functions

Source: Internet
Author: User

Seven little-known but super-practical PHP functions and little-known php Functions

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:

123 <?phphighlight_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:

1234 <?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:

12345 <?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:

1 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:

12345 <?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:

1234567 <?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 with deleted PHP comments and blank characters, which is useful for comparing the actual number of codes with the number of comments.

Example:

123456789 <?php// PHP comment here/* * Another PHP comment */echophp_strip_whitespace(__FILE__);// Newlines are considered whitespace, and are removed too:do_nothing();?>

Output result:

12 <?php echo php_strip_whitespace(__FILE__); do_nothing(); ?>

From: Cats Who Code


Can I use hyperlinks to call php functions on this page?

You can send a parameter to point it to this page ~ For example, myinfo. php? Act = xxx
Then this page starts to judge the act value. If it is xxx, execute the function you want to execute.

Help me find a little-known but funny sketch

Yuesce, yueshui, started with a funny sketch by Wang Han, Mark Chen, for reference.

I used to draw lessons from an exam essay to get high praise.

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.