Home php教程 php手册 几个实用的PHP内置函数使用指南

几个实用的PHP内置函数使用指南

Jun 06, 2016 pm 08:16 PM
php built-in functions

本文给大家推荐了7个不经常被用到,但实际很实用,功能很强大的php内置函数,用好了,可以省去小伙伴们很多的时间的。

PHP有许多内置函数,其中大多数函数都被程序员广泛使用。但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数。 没用过的程序员不妨过来看看。

  1.highlight_string()

  当需要在一个网站中展示PHP代码时,highlight_string()函数就变的非常有用了。该函数通过使用PHP语法高亮程序中定义的颜色,输出或返回给定的PHP代码的语法高亮版本。

  示例:

复制代码 代码如下:


highlight_string('');
?>

  2.str_word_count()

  该函数必须要传递一个参数,根据参数类型返回单词的个数。如下面的所示:

复制代码 代码如下:


$str = "How many words do I have?";
echo str_word_count($str); //Outputs 6
?>

  3.levenshtein()

  该函数主要返回两个字符串之间的Levenshtein距离。Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。该函数对查找用户所提交的错别字非常有用。

  示例:

复制代码 代码如下:


$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>

  4.get_defined_vars()

  该函数返回一个包含所有已定义变量列表的多维数组,这些变量包括环境变量、服务器变量和用户定义的变量。

  示例:

复制代码 代码如下:


print_r(get_defined_vars());

  5.escapeshellcmd()

  该函数用来避开字符串中的特殊符号,可以防止使用者耍花招来破解服务器系统。可以用本函数搭配exec() 或是system() 二个函数,这样可以减少网上使用者的恶意破坏行为。

  示例:

复制代码 代码如下:


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

  6.checkdate()

  本函数可以用来检查日期是否有效,例如年为0至32767年、月为1至12月、日则随着月份及闰年变化。

  示例:

复制代码 代码如下:


var_dump(checkdate(12, 31, 2000));
var_dump(checkdate(2, 29, 2001));
//Output
//bool(true)
//bool(false)
?>

  7.php_strip_whitespace()

  该函数可以返回已删除PHP注释以及空白字符的源代码文件,这对实际代码数量和注释数量的对比很有用。

  示例:

复制代码 代码如下:


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

  输出结果:

复制代码 代码如下:


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

以上7个php的内置函数,,小伙伴们你们用过几个?估计大多数人都没用过吧,实际此类内置函数还有挺多,这里先给大家介绍这7个,后续我们再补上其他(小编回去也要翻翻再总结,真心用的少啊)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles