简体   繁体   English

关于PHP代码的困惑

[英]Confusion about php code

Apologies if this is basic, but I'm learning php. 抱歉,如果这是基本的,但我正在学习php。

What does this snippet of code actually do? 此代码段实际上是做什么的? I've seen it in the source code for a plugin but can't quite figure out what's going on 我已经在插件的源代码中看到了它,但无法完全了解发生了什么

$_POST['newName'] = $_POST['newName'] == "" ? "Dude" : $_POST['newName'];

Thanks. 谢谢。

This is a short version of if...else. 这是if ... else的简短版本。 This is a Ternary Logic . 这是Ternary Logic

$_POST['newName'] = $_POST['newName'] == "" ? "Dude" : $_POST['newName'];

if $_POST['newName'] == "" is true then "Dude" and else $_POST['newName'] . 如果$_POST['newName'] == ""为true, $_POST['newName'] "Dude" ,否则$_POST['newName']

and both the value will be set in $_POST['newName'] . 并且两个值都将在$_POST['newName']

You can write this like this: [Full form] 您可以这样编写:[完整格式]

if($_POST['newName'] == "") 
    $_POST['newName'] = "Dude";

The ? is also known as the ternary operator. 也称为三元运算符。 It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. 之所以称为三元运算符,是因为它需要三个操作数-一个条件,结果为true以及结果为false。 If that sounds like an if statement to you, you are right on the money - the ternary operator is a shorthand (albeit very hard to read) way of doing if statements. 如果这听起来像是if语句,则说明您是对的-三元运算符是if语句的一种简写方式(尽管很难读)。 Here's an example: 这是一个例子:

   <?php
   $agestr = ($age < 16) ? 'child' : 'adult';
    ?>

First there is a condition ($age < 16), then there is a question mark, and then a true result, a colon, and a false result. 首先是一个条件($ age <16),然后是一个问号,然后是真结果,冒号和假结果。 If $age is less than 16, $agestr will be set to 'child', otherwise it will be set to 'adult'. 如果$ age小于16,则$ agestr将设置为“儿童”,否则将设置为“成人”。 That one-liner ternary statement can be expressed in a normal if statement like this: 一线三元语句可以用普通的if语句表示,如下所示:

<?php
if ($age < 16) {
    $agestr = 'child';
} else {
    $agestr = 'adult';
}

?> So, in essence, using the ternary operator allows you to compact five lines of code into one, at the expense of some readability. ?>因此,从本质上讲,使用三元运算符使您可以将五行代码压缩为一行,但会牺牲一些可读性。

Sometimes while coding, you might feel that writing an if(...){...}else{...} might feel like overkill for small amounts of code: 有时,在编码时,您可能会觉得编写if(...){...}else{...}对于少量的代码可能会感觉过分了:

$result;
if (20>3)
{
  $result = "bigger!";
}
else
{
  $result = "smaller!";
}

So for this reason, a short hand notation was created where you would be able to express the exact same statement but without the need for such a big structure: 因此,由于这个原因,创建了一个简短的符号 ,您可以表达完全相同的语句,而无需这么大的结构:

$result = (20>3) ? "bigger!" : "smaller!" ;

Whatever is between = and ? =和之间的任何一个? would then be the condition that is normally between the ( and ) of if(...) . 然后将是通常在if(...)()之间的条件。 If that expression then equates to true , the value obtained by $result would be: "bigger!" 如果该表达式等于true ,则$result获得的值将是: "bigger!" , and if it equated to false , the result would be: "smaller!" ,如果它等于false ,结果将是: "smaller!" .

As Frayne said, it is the shortened version of conditional statement. 正如Frayne所说,它是条件语句的简化版本。 When we write it in full form, it becomes this: 当我们以完整形式编写它时,它变为:

<?php
    if($_POST['newName'] == "") {
        $_POST['newName'] = "Dude";
    } else {
        $_POST['newName'] = $_POST['newName'];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM