7 little-known but very useful PHP functions_PHP Tutorial
7 little-known but very practical PHP functions
This article mainly introduces 7 little-known but very practical PHP functions. This article summarizes such as highlight_string( ), str_word_count(), levenshtein(), get_defined_vars(), escapeshellcmd() and other very useful but uncommon functions. Friends who need them can refer to them
Overview
PHP has numerous built-in functions, most of which are widely used by developers. But there are also some that are equally useful but forgotten in the corner. This article will introduce 7 little-known but very cool functions.
Highlight_string()
When PHP code needs to be displayed on a web page, the highlight_string() function is very useful. This function returns a highlighted version of the code in the function using the colors defined built-in to PHP.
The code is as follows:
Highlight_string('');
echo highlight_string('',true);
?>
str_word_count()
This function can conveniently return the number of words in the input string parameter.
The code is as follows:
$str = "hello world";
echo str_word_count($str); //Output 2
?>
levenshtein()
This function can conveniently return the levenshtein (edit distance) between two parameters. I once encountered a requirement. When users edit their ID cards, they are limited to modifying only 4 digits. This function is used.
The code is as follows:
$idcard='230406198506206797';
$newIdcard='230406198506207798';
echo levenshtein($idcard,$newIdcard); //Output 2
?>
get_defined_vars()
This function is very useful when debugging a program. It will return an array containing all defined variables, including environment, system and user-defined variables.
The code is as follows:
var_dump(get_defined_vars());
?>
escapeshellcmd()
This function is used to skip special symbols in the string to prevent malicious users from playing tricks to crack the server system. Can be used with the exec() and system() functions.
The code is as follows:
$command = './configure '.$_POST['configure_options'];
$escaped_command = escapeshellcmd($command);
system($escaped_command);
?>
checkdate()
This function can be used to check the validity of date parameters. It can verify the validity of each parameter entered.
The code is as follows:
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 with comments and spaces removed. This is useful for comparing the actual amount of code versus the number of comments.
The code is as follows:
// Note 1
/*
* Note 2
*/
echo php_strip_whitespace(__FILE__);
do_nothing();
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
