帮我理解这段代码[重复]

时间:2021-10-03 22:45:03

Possible Duplicate:
What is the PHP ? : operator called and what does it do?

可能重复:什么是PHP? :操作员调用,它做了什么?

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

Can someone help me understand the above code? I'm fairly new to php :) What up with ? and :?

有人可以帮我理解上面的代码吗?我是相当新的PHP :)怎么了?并且:?

I would appreciate it!

我会很感激!

13 个解决方案

#1


This is a ternary operator. This basically says

这是一个三元运算符。这基本上说

if(isset($_GET['id']))
{
   $id = intval($_GET['id']);
}
else
{
   $id = 0;
}

#2


That is a ternary operator.

那是一个三元运算符。

What is says that is if $_GET['id'] is set, $id is intval($_GET['id']), otherwise, $id is 0.

说的是,如果设置$ _GET ['id'],$ id是intval($ _ GET ['id']),否则$ id是0。

#3


The ? and : are parts of an inline if.

的?和:是内联的部分。

Basically, if isset($_GET['id']) is true, intval($_GET['id']) is used. Otherwise, $id gets 0.

基本上,如果isset($ _ GET ['id'])为真,则使用intval($ _ GET ['id'])。否则,$ id为0。

#4


x ? y : z = if x is true then y else z

X ? y:z =如果x为真,则y为z

#5


That’s the conditional operator :

这是条件运算符:

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

表达式(expr1)? (expr2):如果expr1的计算结果为TRUE,则(expr3)求值为expr2;如果expr1求值为FALSE,则求expr3。

#6


It means exactly this:

这意味着:

$id = 0;
if(isset($_GET['id'])) {
    $id = intval($_GET['id'];
}

#7


This is short notation for an if. The notation is taken from C.

这是if的简短表示法。符号取自C.

It could be re-written:

它可以重写:

if (isset($_GET['id']) ) {
     $id = intval($_GET['id']);
} else {
     $id = 0;
}

#8


Its called a ternary

它被称为三元组

it populates $id with intval($_GET['id']) if isset($_GET['id']) returns true else it will populate it with 0

如果isset($ _ GET ['id'])返回true,它会用intval($ _ GET ['id'])填充$ id,否则它会用0填充它

#9


If $_GET['id'] exists it sets $id = $_GET['id'], if not it sets $id = 0, it uses ternary. http://uk3.php.net/ternary

如果$ _GET ['id']存在,则设置$ id = $ _GET ['id'],如果没有设置$ id = 0,则使用三元组。 http://uk3.php.net/ternary

#10


That's a ternary operator. Basically, it has a

那是一个三元运营商。基本上,它有一个

if (condition) {

} else {

}

in one line.

在一条线上。

The code says

代码说

If the GET var id has been set, then set the $id var to equal the integer of the GET's variable.

如果已设置GET var id,则将$ id var设置为等于GET变量的整数。

For arguments sake too, casting with (int) has been proven to be much faster.

为了论证,使用(int)进行转换已经证明要快得多。

#11


That statement essentially means this:

该声明基本上意味着:

$id = 0;

if (isset($_GET['id']))
{
    $id = intval($_GET['id']);
}
else
{
    $id = 0;
}

The ?: operator means "if condition then result else other_result," all in one line. You're basically setting the value of the $id variable based on a boolean (true/false) condition. If the condition is true, the first result is used to set the value of the $id variable. Otherwise, it uses the second value.

?:运算符表示“如果条件然后结果,则else_result”,全部在一行中。您基本上是根据布尔(true / false)条件设置$ id变量的值。如果条件为真,则第一个结果用于设置$ id变量的值。否则,它使用第二个值。

#12


This is just shorthand for an if statement (ternary operator) and is the same as:

这只是if语句(三元运算符)的简写,与以下内容相同:

if (isset($_GET['id']))
{
    $id = intval($_GET['id']);
}
else
{
    $id = 0;
}

#13


see: http://www.lizjamieson.co.uk/2007/08/20/short-if-statement-in-php/ and http://php.net/operators.comparison

请参阅:http://www.lizjamieson.co.uk/2007/08/20/short-if-statement-in-php/和http://php.net/operators.comparison

#1


This is a ternary operator. This basically says

这是一个三元运算符。这基本上说

if(isset($_GET['id']))
{
   $id = intval($_GET['id']);
}
else
{
   $id = 0;
}

#2


That is a ternary operator.

那是一个三元运算符。

What is says that is if $_GET['id'] is set, $id is intval($_GET['id']), otherwise, $id is 0.

说的是,如果设置$ _GET ['id'],$ id是intval($ _ GET ['id']),否则$ id是0。

#3


The ? and : are parts of an inline if.

的?和:是内联的部分。

Basically, if isset($_GET['id']) is true, intval($_GET['id']) is used. Otherwise, $id gets 0.

基本上,如果isset($ _ GET ['id'])为真,则使用intval($ _ GET ['id'])。否则,$ id为0。

#4


x ? y : z = if x is true then y else z

X ? y:z =如果x为真,则y为z

#5


That’s the conditional operator :

这是条件运算符:

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

表达式(expr1)? (expr2):如果expr1的计算结果为TRUE,则(expr3)求值为expr2;如果expr1求值为FALSE,则求expr3。

#6


It means exactly this:

这意味着:

$id = 0;
if(isset($_GET['id'])) {
    $id = intval($_GET['id'];
}

#7


This is short notation for an if. The notation is taken from C.

这是if的简短表示法。符号取自C.

It could be re-written:

它可以重写:

if (isset($_GET['id']) ) {
     $id = intval($_GET['id']);
} else {
     $id = 0;
}

#8


Its called a ternary

它被称为三元组

it populates $id with intval($_GET['id']) if isset($_GET['id']) returns true else it will populate it with 0

如果isset($ _ GET ['id'])返回true,它会用intval($ _ GET ['id'])填充$ id,否则它会用0填充它

#9


If $_GET['id'] exists it sets $id = $_GET['id'], if not it sets $id = 0, it uses ternary. http://uk3.php.net/ternary

如果$ _GET ['id']存在,则设置$ id = $ _GET ['id'],如果没有设置$ id = 0,则使用三元组。 http://uk3.php.net/ternary

#10


That's a ternary operator. Basically, it has a

那是一个三元运营商。基本上,它有一个

if (condition) {

} else {

}

in one line.

在一条线上。

The code says

代码说

If the GET var id has been set, then set the $id var to equal the integer of the GET's variable.

如果已设置GET var id,则将$ id var设置为等于GET变量的整数。

For arguments sake too, casting with (int) has been proven to be much faster.

为了论证,使用(int)进行转换已经证明要快得多。

#11


That statement essentially means this:

该声明基本上意味着:

$id = 0;

if (isset($_GET['id']))
{
    $id = intval($_GET['id']);
}
else
{
    $id = 0;
}

The ?: operator means "if condition then result else other_result," all in one line. You're basically setting the value of the $id variable based on a boolean (true/false) condition. If the condition is true, the first result is used to set the value of the $id variable. Otherwise, it uses the second value.

?:运算符表示“如果条件然后结果,则else_result”,全部在一行中。您基本上是根据布尔(true / false)条件设置$ id变量的值。如果条件为真,则第一个结果用于设置$ id变量的值。否则,它使用第二个值。

#12


This is just shorthand for an if statement (ternary operator) and is the same as:

这只是if语句(三元运算符)的简写,与以下内容相同:

if (isset($_GET['id']))
{
    $id = intval($_GET['id']);
}
else
{
    $id = 0;
}

#13


see: http://www.lizjamieson.co.uk/2007/08/20/short-if-statement-in-php/ and http://php.net/operators.comparison

请参阅:http://www.lizjamieson.co.uk/2007/08/20/short-if-statement-in-php/和http://php.net/operators.comparison