isset PHP isset($_GET['something']) ? $_GET['东西'] : ''

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12126420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:53:19  来源:igfitidea点击:

isset PHP isset($_GET['something']) ? $_GET['something'] : ''

phpisset

提问by user1625186

I am looking to expand on my PHP knowledge, and I came across something I am not sure what it is or how to even search for it. I am looking at php.net isset code, and I see isset($_GET['something']) ? $_GET['something'] : ''

我希望扩展我的 PHP 知识,但我遇到了一些我不确定它是什么或如何搜索它的东西。我在看 php.net isset 代码,我看到isset($_GET['something']) ? $_GET['something'] : ''

I understand normal isset operations, such as if(isset($_GET['something']){ If something is exists, then it is set and we will do something }but I don't understand the ?, repeating the get again, the : or the ''. Can someone help break this down for me or at least point me in the right direction?

我理解正常的 isset 操作,例如if(isset($_GET['something']){ If something is exists, then it is set and we will do something }但我不理解 ?、再次重复 get、: 或 ''。有人可以帮我分解一下,或者至少为我指明正确的方向吗?

回答by u?nb??s

It's commonly referred to as 'shorthand' or the Ternary Operator.

它通常被称为“速记”或三元运算符

$test = isset($_GET['something']) ? $_GET['something'] : '';

means

方法

if(isset($_GET['something'])) {
    $test = $_GET['something'];
} else {
    $test = '';
}

To break it down:

分解:

$test = ... // assign variable
isset(...) // test
? ... // if test is true, do ... (equivalent to if)
: ... // otherwise... (equivalent to else)

Or...

或者...

// test --v
if(isset(...)) { // if test is true, do ... (equivalent to ?)
    $test = // assign variable
} else { // otherwise... (equivalent to :)

回答by sciritai

That's called a ternary operator and it's mainly used in place of an if-else statement.

这称为三元运算符,主要用于代替 if-else 语句。

In the example you gave it can be used to retrieve a value from an array given isset returns true

在您给出的示例中,它可用于从数组中检索值,给定 isset 返回 true

isset($_GET['something']) ? $_GET['something'] : ''

is equivalent to

相当于

if (isset($_GET['something'])) {
  $_GET['something'];
} else {
  '';
}

Of course it's not much use unless you assign it to something, and possibly even assign a default value for a user submitted value.

当然,除非您将其分配给某些东西,否则它没有多大用处,甚至可能为用户提交的值分配一个默认值。

$username = isset($_GET['username']) ? $_GET['username'] : 'anonymous'

回答by George Garchagudashvili

From php 7 you can write it even shorter:

从 php 7 你可以写得更短:

$age = $_GET['age']) ?? 27;

And this means if age param is provided in the url it will be set to $agevar, or will default to 27

这意味着如果在 url 中提供了年龄参数,它将被设置为$agevar,或者默认为 27

See all new features of php 7

查看php 7 的所有新特性

回答by FThompson

You have encountered the ternary operator. It's purpose is that of a basic if-else statement. The following pieces of code do the same thing.

您遇到了三元运算符。它的目的是一个基本的 if-else 语句。下面几段代码做同样的事情。

Ternary:

三元:

$something = isset($_GET['something']) ? $_GET['something'] : "failed";

If-else:

如果别的:

if (isset($_GET['something'])) {
    $something = $_GET['something'];
} else {
    $something = "failed";
}

回答by mathematician1975

It is called the ternary operator. It is shorthand for an if-else block. See here for an example http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

它被称为三元运算符。它是 if-else 块的简写。参见这里的示例http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

回答by Nikola Ninkovic

? is called Ternary (conditional) operator : example

? 称为三元(条件)运算符:示例

回答by bmorenate

What you're looking at is called a Ternary Operator, and you can find the PHP implementation here. It's an if elsestatement.

您正在查看的称为三元运算符,您可以在此处找到PHP 实现。这是一个if else声明。

if (isset($_GET['something']) == true) {
    thing = isset($_GET['something']);
} else {
    thing = "";
}

回答by TrophyGeek

If you want an empty string default then a preferred way is one of these (depending on your need):

如果您想要一个空字符串默认值,那么首选方法是其中之一(取决于您的需要):

$str_value = strval($_GET['something']);
$trimmed_value = trim($_GET['something']);
$int_value = intval($_GET['somenumber']);

If the url parameter somethingdoesn't exist in the urlthen $_GET['something']will return null

如果URL参数something并不在URL中存在,然后$_GET['something']将返回null

strval($_GET['something'])-> strval(null)-> ""

strval($_GET['something'])-> strval(null)->""

and your variable $valueis set to an empty string.

并且您的变量$value设置为空字符串。

  • trim()might be prefered over strval()depending on code (e.g. a Name parameter might want to use it)
  • intval()if only numeric values are expected and the default is zero. intval(null)-> 0
  • trim()strval()根据代码可能更受欢迎(例如 Name 参数可能想要使用它)
  • intval()如果只需要数字值并且默认值为零。intval(null)->0

Cases to consider:

需要考虑的情况:

...&something=value1&key2=value2(typical)

...&something=value1&key2=value2(典型的)

...&key2=value2(parameter missing from url $_GET will return null for it)

...&key2=value2(url $_GET 中缺少的参数将为其返回 null)

...&something=+++&key2=value(parameter is " ")

...&something=+++&key2=value(参数为" "

Why this is a preferred approach:

为什么这是首选方法:

  • It fits neatly on one line and is clear what's going on.
  • It's readable than $value = isset($_GET['something']) ? $_GET['something'] : '';
  • Lower risk of copy/paste mistake or a typo: $value=isset($_GET['something'])?$_GET['somthing']:'';
  • It's compatible with older and newer php.
  • 它整齐地排列在一条线上,并且很清楚发生了什么。
  • 它的可读性比 $value = isset($_GET['something']) ? $_GET['something'] : '';
  • 降低复制/粘贴错误或拼写错误的风险: $value=isset($_GET['something'])?$_GET['somthing']:'';
  • 它与旧的和新的 php 兼容。


UpdateStrict mode may require something like this:

更新严格模式可能需要这样的东西:

$str_value = strval(@$_GET['something']);
$trimmed_value = trim(@$_GET['something']);
$int_value = intval(@$_GET['somenumber']);