search
Homephp教程php手册了解这些PHP小技巧吗?

1. $_POST并非是HTTP POST过来的数据 , 如json格式的数据就没法接受,这是因为由于历史原因,PHP只能解析Content-Type为 application/x-www-form-urlencoded 或 multipart/form-data的Http请求,只不过现在json流行了,如下处理即可: $_POST = json_decode

1. $_POST并非是HTTP POST过来的数据, 如json格式的数据就没法接受,这是因为由于历史原因,PHP只能解析Content-Type为 application/x-www-form-urlencoded 或 multipart/form-data的Http请求,只不过现在json流行了,如下处理即可:

$_POST = json_decode(file_get_contents('php://input'), true);
print_r($_POST);

2.isset()函数是用来检测变量是否设置,但是如果变量值是NULL时也返回false。 这可能跟你的本意相反了,如下解决:
$a = null;
echo isset($a);  //false
//下面是更可靠的检测方式
echo array_key_exists('a', get_defined_vars());  //true

3.如何更有效的检测字符串长度。一般strlen函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步骤,如字母小写化(译注:指函数名小写化,PHP不区分函数名大小写)、哈希查找,会跟随被调用的函数一起执行。在某些情况下,你可以使用isset() 技巧加速执行你的代码。

if (strlen($foo) <br><p>调用isset()恰巧比strlen()快,因为与后者不同的是,isset()作为一种语言结构,意味着它的执行不需要函数查找和字母小写化。也就是说,实际上在检验字符串长度的顶层代码中你没有花太多开销。</p>
    <p class="copyright">
        原文地址:了解这些PHP小技巧吗?, 感谢原作者分享。
    </p>
    
    


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

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft