null合并运算符是一个好东西,有了它我们就能很方便的获取一个参数,并能在其为空的情况下提供一个默认值。比如在js中可以用||来搞:
function setSomething(a){ a = a || 'some-default-value'; // ... }
而在PHP中,可惜PHP的||总是返回true或false,无法这样来搞。
PHP7才正式加入了??这个运算符:
// 获取user参数的值(如果为空,则用'nobody') $username = $_GET['user'] ?? 'nobody'; // 等价于: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
PHP7 估计还有很长时间才能用到生产环境中,那在目前的PHP5中有没有替代方案呢?
据研究,完全有个非常便捷的替代方案:
// 获取user参数的值(如果为空,则用'nobody') $username = @$_GET['user'] ?: 'nobody'; // 等价于: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
-- 运行此代码: https://3v4l.org/aDUW8
瞪大了眼睛看,跟前面的PHP7的例子差不多,主要是把 ?? 替换为了 ?: 。 这个是个什么鬼呢?其实这就是 (expr1) ? (expr2) : (expr3) 表达式的省略模式:
表达式 (expr1) ? (expr2) : (expr3) 在 expr1 求值为 TRUE 时的值为 expr2,在 expr1 求值为 FALSE 时的值为 expr3。
自 PHP 5.3 起,可以省略三元运算符中间那部分。表达式 expr1 ?: expr3 在 expr1 求值为 TRUE 时返回 expr1,否则返回 expr3。
-- http://php.net/manual/zh/language.operators.comparison.php
当然,这个替代方案也不是完美的 —— 如果 $_GET 中没有 'user',会有一条 Notice: Undefined index: user 的错误,所以需要用@来抑制这个错误,或者关闭 E_NOTICE 的错误。
ps:PHP7 null合并运算符 告别isset()
以前的写法
$info = isset($_GET['email']) ? $_GET['email'] : ‘noemail';
现在直接这样写就可以了
$info = $_GET['email'] ?? noemail;
还可以这样连写使用
$info = $_GET['email'] ?? $_POST['email'] ?? ‘noemail';

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

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.

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function