Seven little-known but extremely useful PHP Functions

Source: Internet
Author: User

PHP has many built-in functions, most of which are widely used by developers. However, there are also some functions that are useful but forgotten in the corner. This article will introduce seven functions that are little-known but cool.

1. highlight_string ()

When PHP code needs to be displayed on a webpage, The highlight_string () function is very useful. This function uses the color defined in PHP to return the highlighted version of the Code in the function.

Usage:

<?phphighlight_string('<?php phpinfo(); ?>');?>
2. str_word_count ()

This function can easily return the number of words in the input string parameter.

Usage:

?php$str = "How many words do I have?";echo str_word_count($str); //Outputs 5?>
3. levenshtein ()

To track spelling errors, this function can easily return levenshtein between two parameters ).

Usage:

<?php$str1 = "carrot";$str2 = "carrrott";echo levenshtein($str1, $str2); //Outputs 2?>
4. get_defined_vars ()

This function is useful when debugging a program. It returns an array containing all the Defined variables, including the environment, system, and User-Defined variables.

Usage:

print_r(get_defined_vars());

5. escapeshellcmd ()

This function is used to skip special characters in a string to prevent malicious users from cracking the server system. It can be used with exec () and system () functions.

Usage:

<?php$command = './configure '.$_POST['configure_options'];$escaped_command = escapeshellcmd($command); system($escaped_command);?>
6. checkdate ()

This function can be used to check the validity of the date parameter. It verifies the validity of each input parameter.


Usage:

<?phpvar_dump(checkdate(12, 31, 2000));var_dump(checkdate(2, 29, 2001));//Output//bool(true)//bool(false)?>
7. php_strip_whitespace ()

This function returns the PHP source code after the comments and spaces are deleted. This is useful for comparing the actual number of codes and the number of comments.

Usage:

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

The output:

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



========================================================== ============================

PHP platform activated!
Search for "PHPChina" and click "follow" to obtain the latest and most professional industry information pushed by PPC. More topics will be provided for you.
[PPC mining]: provides you with stories about classic products and product people from time to time.
[Ppc foreign language]: Share a foreign language translation article every day.

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.