search
Homephp教程php手册如何使用PHP运算符==比较字符串

如何使用PHP运算符==比较字符串

Jun 13, 2016 am 11:07 AM
phptruecodeuseexisthowstringrightCompareoutputoperator

以上代码输出结果为:

bool(true)所以,在使用对字符串进行比较时,建议使用PHP运算符==对字符串进行严格的检查,或使用strcmp()等函数,从而避免可能产生的问题。

除此之外,常用的in_array()函数也存在弱类型的问题,见如下代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li><span>var_dump(in_array('01', array('1')));  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>

以上代码输出结果为:

bool(true)

相信用过该函数进行安全性检查的PHP编程人员都知道这会产生怎么样的安全问题了吧?幸好in_array()函数为我们提供了第三个参数,把它设为 true 就可以打开in_array()函数的强制类型检查机制,如下代码所示:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li><span>var_dump(in_array('01', array('1'), true));   </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>

输出结果为:

bool(false)

由于PHP是一种弱类型的语言,也就是说数据类型这个概念在PHP中被弱化。因而如果在编程时过分忽略数据类型(也是大部份PHP程序员的通病),会产生一些问题,甚至导致安全漏洞。在PHP运算符==的介绍最后,还是那句说得很烦很烦的话,对外来数据进行严格检查和过滤。


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

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.