7 little-known but very useful PHP functions _php tips

Source: Internet
Author: User
Tags php source code

Overview

PHP has a number of built-in functions, most of which are used by developers. But there are some equally useful but forgotten in the corner, this article will introduce 7 little-known features but very cool functions.

Highlight_string ()

The highlight_string () function is useful when you need to display PHP code in a Web page. This function returns the highlighted version of the code in the function through the color defined in PHP.

Copy Code code as follows:

<?php
Highlight_string (' <?php echo ' Hello World;?> ');
echo highlight_string (' <?php echo ' Hello World;?> ', true);
?>

Str_word_count ()

This function makes it easy to return the number of words in the input string argument.

Copy Code code as follows:

<?php
$str = "Hello World";
echo Str_word_count ($STR); Output 2
?>

Levenshtein ()

The function can easily return the Levenshtein (edit distance) between two parameters. Once encountered a need, users in the editing of identity cards, restricting users can only modify 4 digits, using this function.

Copy Code code as follows:

<?php
$idcard = ' 230406198506206797 ';
$newIdcard = ' 230406198506207798 ';
Echo Levenshtein ($idcard, $newIdcard); Output 2
?>

Get_defined_vars ()

This function is useful when debugging a program, and it returns an array of all the defined variables, including the environment, system, and user-defined variables.

Copy Code code as follows:

<?php
Var_dump (Get_defined_vars ());
?>

Escapeshellcmd ()
This function is used to skip the special symbols in the string to prevent malicious users from playing tricks on the server system. Can be used with the Exec () and system () functions.

Copy Code code as follows:

<?php
$command = './configure '. $_post[' configure_options '];
$escaped _command = Escapeshellcmd ($command);
System ($escaped _command);
?>

Checkdate ()

This function can be used to detect the validity of date parameters. It verifies the legality of each parameter entered.

Copy Code code as follows:

<?php
Var_dump (Checkdate (12, 31, 2000));
Var_dump (Checkdate (2, 29, 2001));
Output
BOOL (TRUE)
BOOL (FALSE)
?>

Php_strip_whitespace ()

This function will return the PHP source code after the deletion of the annotation and space. This is useful for comparing the actual number of code with the number of comments.

Copy Code code as follows:

<?php
Note 1
/*
* NOTE 2
*/
echo Php_strip_whitespace (__file__);
Do_nothing ();
?>

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.