Php一条线需要在底层[复制]

This question already has an answer here:

what is the meaning of this line? Can any body help me? I have confusion on last 2 sign :'' why without this two sign browser make error? Thanks all.

isset($_POST['but'])? $_POST['but']:'';
</div>

It's just another way to write if/else statement. For example:

$but = isset($_POST['but'])? $_POST['but']:'';

Would be the same as:

if(isset($_POST['but'])){
$but= $_POST['but'];
}else{
$but = '';
}

If $_POST request body contains but parameter - use it, overwise use empty string.

If you are misunderstood statement ? something : anything code - it's the Ternary Operator

It's nothing but the Ternary Operator

Explanation: If your $_POST['but'] is set , then it will assign the same value to it , else it will set a ''

A clear example here

Well it's called an ternary operator.

Example:

$value = 5;
($value > 2) ? true : false; // returns true

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

(isset($_POST['but'])) ? $_POST['but']:'';

Here if your value $_POST['but'] is set then it will return $_POST['but'] else it will return ''.

U use the tenary comparison operator

A ternary operator have value for true and false

($contidition) ? true : false;

Please refer php documentation about ternary operator comparison operator

In case of:

isset( $_POST['but'] ) ? $_POST['but'] : ''

What it mean is, when $_POST['but'] exist, use it, otherwise use empty string

If u use php version > 5.3 u can use something like

isset($_POST['but']) ? : ''