search
Homephp教程php手册PHP中如何判断字符串为空?

PHP中如何判断字符串为空?

Jun 10, 2016 pm 03:09 PM
phpcodestringOpen sourceprogrammingprogramming languagesoftware development

 

 

在开发淘宝SDK模板时,在module.xml中经常需要配置变量以便于用户进行模板的编辑。但难免用户在输入参数时,忘记了输入参数。此是,我们就得使用在PHP代码中加入一段判断用户是否输入了参数的代码,常用的有两种方式:

方式一:使用$_MODULE['变量名']的方式

if($_MODULE['var']) {
     // 这是表示已经输入了值
}

else {
     // 这里表示未输入值
}

 这种方法很简单,我也一直常用。

 

方式二:先赋值,再用empty判断。

但有时候,为了代码的紧凑性,经常需要在模块的PHP文件中的最前面初始化变量的值。然后再逐一判断用户哪些输入是有效的,如果出现了错误,那就应该在模块中展示出错误给用户。如下:

$mod_title = $_MODULE['title'];
$mod_style = $_MODULE['style'];
$mod_href = $_MODULE['href'];

// ……

 为了不使模板出现不正常的现象,我们可以需要对$mod_style进行判断,只是指定了$mod_style的值时,才能根据它对模板进行展示。此时,一般是直接对变量$mod_style进行判断了,而我们最常见的错误方式是:

if($mod_style == "") { //.... }

 或者是:

if($mod_style) { //.... }

 在实际的应用当中,却并不能如我们所愿,在我亲自测试之后发现,不论使用上面哪种方式,即使我不输入任何值,if后面的条件值总是为true。所以,我一般将其改写成了如下形式:

if($_MODULE['style']) { //... }

 如此一来便能正确的解决问题,但是输入 $mod_style 比输入 $_MODULE['style'] 更加迅速,也更不容易出错,所以我还是希望使用$mod_style来判断,结果采用了如下代码:

if(!empty($mod_style)) { // .... }

 经测试,正常了!

Statement
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.