php性能优化:使用 isset()判断字符串长度速度比strlen()更快
php中如何判断字符串的长度?很多人第一想到的应该就是 strlen() mb_strlen() 这些函数了。但是从程序性能上来讲,这两个函数在判断字符串长度的过程中都不是最佳的,虽然他们是专业级的检测字符串长度的函数。
通过本人实践总结,php判断字符串长度,使用isset()在速度上比strlen()更快,执行效率更高。
那么isset()速度为什么比strlen()更快呢?
strlen()函数函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步骤,如字母小写化、哈希查找,会跟随被调用的函数一起执行。因此在某些情况下,合理使用isset()可以加速你的程序。因为isset()是一种语言结构,它的执行不需要函数查找和字母小写化等。
具体通过 isset() 和 strlen() 判断字符串长度示例如下:
$str='http://www.phpernote.com/php-template/436.html'; if(strlen($str)<5){echo "未满5";} if(!isset($str{5})){echo "未满5";}
下面我们来详细的分析一下strlen()和isset()这两个函数。
PHP strlen() 函数
定义和用法
strlen() 函数返回字符串的长度。
语法:strlen(string)
参数:string
描述:必需。规定要检查的字符串。
strlen() 函数实例
<?php echo strlen("Hello world!"); ?>
结果将输出:
12
PHP isset() 函数
isset函数是检测变量是否设置。
语法:bool isset ( mixed var [, mixed var [, ...]] )
返回值:
若变量不存在则返回 FALSE
若变量存在且其值为NULL,也返回 FALSE
若变量存在且值不为NULL,则返回 TURE
同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE
如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是:一个 NULL 字节("")并不等同于 PHP 的 NULL 常数。
警告:isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用defined() 函数。
您可能感兴趣的文章
- php字符串替换函数str_replace速度比preg_replace快
- php最精确的字符串长度截取函数
- php被遗忘的一些功能强大的字符串处理函数
- php获取utf8字符串的字符长度
- PHP中的字符串处理函数(String Functions) 全总结
- php判断字符串是否全英文,纯中文,中英文组合的方法
- PHP 字符串转义函数(addslashes,stripslashes)详解
- php 将字符串中的连续多个空格转换为一个空格

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

Many friends complain about the slow download speed of Uplay, and the download speed of Uplay platform is too slow. Sometimes it is only a few KB, so how to solve it? It is mainly the problem of dns and hosts files. Below, I will tell you about the slow download speed of Uplay. Let’s take a look at the solutions in detail. Uplay download speed is always 0. Solution 1. Double-click to open this computer! As shown in the picture below 2. Enter this computer and find your system disk. Generally, the default is the c drive, as shown in the picture below 3. Then enter this folder: C:\Windows\System32\drivers\etc\, as shown below As shown in the figure below 4. Right-click the hosts file - select Open method, as shown in the figure below 5. Open

How to solve the slow upload speed of Win10 computer? When we use the computer, we may feel that the file upload speed of our computer is very slow. So what is the situation? In fact, this is because the default upload speed of the computer is 20%, so the upload speed is very slow. Many friends do not know how to operate in detail. The editor has compiled the steps to format the C drive in Win11 below. If you are interested, follow Let’s take a look below! Solution to the slow upload speed of Win10 1. Press win+R to call up run, enter gpedit.msc, and press Enter. 2. Select the management template, click Network--Qos Packet Scheduler, and double-click Limit to reserve bandwidth. 3. Select Enabled, which will bring

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

Laravel is a popular PHP development framework, but it is sometimes criticized for being as slow as a snail. What exactly causes Laravel's unsatisfactory speed? This article will provide an in-depth explanation of the reasons why Laravel is as slow as a snail from multiple aspects, and combine it with specific code examples to help readers gain a deeper understanding of this problem. 1. ORM query performance issues In Laravel, ORM (Object Relational Mapping) is a very powerful feature that allows

As a flagship mobile phone that has attracted much attention, Kirin 9000s has attracted widespread discussion and attention since its launch. It is equipped with the latest flagship chip of the Kirin 9000 series, and its performance is very strong. So, what is the performance of Kirin 9000s? Let’s explore it together. First of all, Kirin 9000s is manufactured using a new 5nm process, which greatly improves the performance and power consumption control of the chip. Compared with previous Kirin processors, Kirin 9000s has significantly improved performance. Whether running big games, multitasking or

Is single-core Google Chrome slower than dual-core browsers? Nowadays, many people are using various browsers to surf the Internet, and Google Chrome is one of them. As the leader in browsers, its technology is unquestionable. Some friends often ask the editor, what do you mean by single- and dual-core browsers? Will this affect browser loading speed? Today I will talk to you about this issue. Single-core and dual-core browser speed PK of Google Chrome First of all, the editor will give you a conclusion: the single-core Google Chrome is not slower than the dual-core browser. The core of the browser is not like a battery, the more there are, the stronger it is. One of the cores of the dual-core browser is the IE browser core, and the other core is WebKit.

In PHP, isset() is a function that checks if a variable is set and not NULL. Returns true if the variable exists and is not NULL, false otherwise.

The running speed of win7 system is closely related to our daily experience when using computers. Whether it is studying, working or watching videos and playing games, the faster the computer, the better. So how to optimize the speed, we can start from many aspects, as follows Let’s take a look together. How to optimize the speed of win7 1. Boot speed 1. We can optimize the boot speed by turning off the startup items. 2. First search and open the "msconfig" program. 3. Check "Selective startup" as shown in the figure. 4. Then uncheck the items that do not need to be started during startup. 2. Optimize performance 1. First use the shortcut key "win+break" to open the system properties interface. 2. Click "Performance Information and Tools" in the lower left corner 3. Check "
