What does ? ... : ... do?(什么?... : ... 做?)
问题描述
$items = (isset($_POST['items'])) ? $_POST['items'] : array();
我不明白这段代码的最后一个片段 "? $_POST['items'] : array();"
I don't understand the last snippet of this code "? $_POST['items'] : array();"
这种代码组合究竟有什么作用?
What does that combination of code do exactly?
我使用它从 html 文本框中获取一堆值并将其存储到会话数组中.但问题是,如果我尝试重新提交文本框中的数据,新的数组会话会完全覆盖旧的会话数组.
I use it to take in a bunch of values from html text boxes and store it into a session array. But the problem is, if I attempt to resubmit the data in text boxes the new array session overwrites the old session array completely blank spaces and all.
我只想覆盖数组中已有值的位置.如果用户决定只填写几个文本框,我不希望之前的会话数组数据被空格覆盖(来自空白文本框).
I only want to overwrite places in the array that already have values. If the user decides to fill out only a few text boxes I don't want the previous session array data to be overwritten by blank spaces (from the blank text boxes).
我认为上面的代码有问题,但我不确定它是如何工作的.请赐教.
I'm thinking the above code is the problem, but I'm not sure how it works. Enlighten me please.
推荐答案
This is a 三元运算符:
表达式 (expr1) ?(expr2) : (expr3) 计算结果为 expr2 如果 expr1 计算结果为 TRUE,并且 expr3如果 expr1 计算结果为 FALSE.
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
这篇关于什么?... : ... 做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么?... : ... 做?
基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
