The null coalescing operator is a good thing. With it, we can easily get a parameter and provide a default value when it is empty. For example, in js, you can use || to do:
function setSomething(a){ a = a || 'some-default-value'; // ... }
. But in PHP, unfortunately, PHP's || always returns true or false, so you can't do it this way.
PHP7 has just officially added this operator:
// 获取user参数的值(如果为空,则用'nobody') $username = $_GET['user'] ?? 'nobody'; // 等价于: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
It is estimated that PHP7 will take a long time to be used in the production environment. So are there any alternatives in the current PHP5?
According to research, there is a very convenient alternative:
// 获取user参数的值(如果为空,则用'nobody') $username = @$_GET['user'] ?: 'nobody'; // 等价于: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
-- Run this code: https://3v4l.org/aDUW8
Look at it with wide eyes, it is similar to the previous PHP7 example, The main thing is to replace ?? with ?:. What the hell is this? In fact, this is the omission pattern of (expr1) ? (expr2) : (expr3) expression:
Expression (expr1) ? (expr2) : (expr3) When expr1 evaluates to TRUE, the value is expr2, and when expr1 evaluates to The value when FALSE is expr3.
Since PHP 5.3, you can omit the middle part of the ternary operator. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.
-- http://php.net/manual/zh/language.operators.comparison.php
Of course, this alternative is not perfect - if there is no 'user' in $_GET, there will be a Notice : Undefined index: user error, so you need to use @ to suppress this error, or turn off the E_NOTICE error.
ps: PHP7 null coalescing operator Say goodbye to isset()
the previous way of writing
$info = isset($_GET['email']) ? $_GET['email'] : ‘noemail';
Now you can just write it like this
$info = $_GET['email'] ?? noemail;
You can also write it in conjunction like this
$info = $_GET['email'] ?? $_POST['email'] ?? ‘noemail';
The above has introduced a detailed explanation of the null coalescing operator in PHP, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

如果您是IT管理员或技术专家,您一定意识到自动化的重要性。尤其对于Windows用户来说,MicrosoftPowerShell是最佳的自动化工具之一。微软为满足您的自动化需求提供了各种工具,无需安装第三方应用程序。本指南将详细介绍如何利用PowerShell自动化执行任务。什么是PowerShell脚本?如果您有使用PowerShell的经验,您可能已经使用过命令来配置您的操作系统。脚本是.ps1文件中这些命令的集合。.ps1文件包含由PowerShell执行的脚本,例如基本的Get-Help

一、java调用post接口1、使用URLConnection或者HttpURLConnectionjava自带的,无需下载其他jar包URLConnection方式调用,如果接口响应码被服务端修改则无法接收到返回报文,只能当响应码正确时才能接收到返回publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

在Linux中,URL或Curl客户端是一个流行的命令行实用程序,允许您使用HTTPS、HTTP、FTP等多种协议在网络上传输数据。它允许您使用其get、post和request方法发送和接收数据。其中,你需要经常使用“get”方法。因此,学习各种方法和各种选项,你可以用来提高你的生产力变得至关重要。“执行卷曲操作非常简单,只需输入几个简单的命令即可完成。尽管这看似简单,但许多用户并未充分认识到其潜力。因此,这篇简短指南提供了一些关于在Linux系统中使用“curlget”命令的实例。”Curl

在Docker中,挂载目录的权限问题通常可以通过以下方法解决:使用-v参数指定挂载目录时添加权限相关的选项。可以通过在挂载的目录后面添加:ro或:rw来指定挂载目录的权限,分别表示只读和读写权限。例如:dockerrun-v/host/path:/container/path:roimage_name在Dockerfile中定义USER指令来指定容器中运行的用户,以确保容器内部的操作符合权限要求。例如:FROMimage_name#CreateanewuserRUNuseradd-ms/bin/

手动修改Ubuntu的apt-get源1、用ssh工具连接到Ubuntu(我用的xshell)2、命令行敲入cd/etc/apt/3、备份此目录下的source.list文件(要有sudo权限),此时就有了一个source.list.bak文件4、清空source.list文件内容(注:清空后不可恢复,所以需要执行上一步提前备份一下这个文件),此时用sudo提示权限不够,直接切换到root用户下执行这条命令5、用vim打开source.list,按i键进入编辑模式把要修改的源地址粘贴进来,然后按

jQuery中get和post是两种常用的ajax请求方法,用于向服务器发送请求并获取数据。它们在使用方式和一些特性上有一些不同,接下来我们将详细解释它们的异同点,并附上具体的代码示例。get和post的相同点:都是用于发送ajax请求的方法,可以通过指定URL和数据参数来从服务器获取数据。都可以接受回调函数作为参数,用于处理服务器返回的数据或处理请求失败的

get和post的区别主要是使用方式、数据传输方式、请求长度限制、安全性、缓存和幂等性等。详细介绍:1、使用方式,GET和POST的主要区别在于使用方式,GET请求用于从服务器获取数据,一般用于获取资源或查询数据,它将请求参数附加在URL的后面,以键值对的形式传递给服务器,POST请求用于向服务器提交数据,一般用于创建、更新或删除资源,它将请求参数放在请求体中等等。

get请求和post请求的区别主要包括幂等性、参数传递方式、安全性和适用场景等。详细介绍:1、幂等性,GET请求是一种幂等的请求,即多次请求相同的URL和参数,结果都是一样的,不会对服务器端产生影响,而POST请求则不是幂等的,多次请求可能会对服务器端产生不同的影响;2、参数传递方式,GET请求将请求的参数以查询字符串的形式附加在URL后面等等。


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.

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
