A few useful guidelines for using PHP built-in functions _php tips

Source: Internet
Author: User

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 you to 7 little-known, but very useful functions. A useless programmer might as well come and see.

1.highlight_string ()

When you need to display PHP code in a Web site, the highlight_string () function becomes very useful. This function outputs or returns a syntax-highlighting version of a given PHP code by using the colors defined in the PHP syntax highlighting program.

Example:

Copy Code code as follows:

<?php
Highlight_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:

Copy Code code 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 called edit distance, refers to the minimum number of edits required between two strings and one converted to another. A licensed editing operation involves replacing one character with another, inserting a character, and deleting a character. This function is useful for finding typos submitted by users.

Example:

Copy Code code as follows:

<?php
$str 1 = "carrot";
$str 2 = "Carrrott";
Echo Levenshtein ($str 1, $str 2); Outputs 2
?>

4.get_defined_vars ()

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

Example:

Copy Code code as follows:

Print_r (Get_defined_vars ());

5.escapeshellcmd ()

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

Example:

Copy Code code 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, such as 0-32,767 year, month 1-12 month, and day with month and leap year.

Example:

Copy Code code as follows:

<?php
Var_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 annotations and whitespace characters, which is useful for comparing the actual number of codes with the number of comments.

Example:

Copy Code code 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 results:

Copy Code code as follows:

<?php
echo Php_strip_whitespace (__file__); Do_nothing ();?>

The above 7 PHP built-in functions, little friends How many did you use? It is estimated that most people have not used it, the actual built-in functions such as there are quite a lot, here first to introduce these 7, follow-up we make up the other (small series back to also want to turn over and summarize, really use less AH)

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.