开发者

What The Result of Array Variable Plus One

I'm looking @ some PHP code, & I found this line, which I couldn't figure out its meaning

            $contents[$item] = (isset($contents[$item]开发者_运维技巧)) ? $contents[$item] + 1 : 1;

so if the condition is evaluated to be true, then what exactly happen, does this increments the array index or, does it add one to the array value??

any explanation or even other resources would be highly appreciated


It's whatever value is inside that array at that specific key, plus 1. For example, if $contents[$item] was 3, it'd become 4. It doesn't mess up the whole $contents array, nor does it change the $item key; it just changes that particular value in the array (if it exists).

Visualize it this way:

$item = 'a';

$contents = array(
    'a' => 3
);

// $contents['a'] is set, so it assigns $contents[$item] to itself, plus 1
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

echo $contents[$item]; // Output is 4


 $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

is functionally equivalent to:

 @$contents[$item]++;

(I'm looking forward to your corrections.)


It's just a fancy way of writing

if (isset($contents[$item]))
  $contents[$item] = $contents[$item] + 1;
else
  $contents[$item] =  1;


Check out the docs for the ternary operator. If the statement is true, it executes the first statement, returning the value of the array + 1. Otherwise, it executes the second statement and returns 1.

But really, read the PHP manual. It is good for you.


actually,

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

is the contracted form of:

if (isset($contents[$item])) {
$contents[$item] = $contents[$item] + 1 
}
else{
$contents[$item] = 1;
}


The process of the line is pretty simple.

Firstly this is a ternary operation using the ? as well as the :, there basically if and else blocks

For example, the following 2 are the exact same

if(true)
{
    someTrueFunction();
}else
{
    someFalseFunction();
}

and

true ? someTrueFunction() : someFalseFunction();

Where the if statements have the following syntax:

if(expression)
{
    //do Work
}else
{
   //Do Other Work
}

the ternary operator does the same but removing braces, and keywords:

expression ? work : other Work

The difference with ternary is that the values within the true / false areas are returned, which allows you to assign data to a variable under a condition, keeping it all in one line, Full Example:

$value = 1;
$ValueValid = $value > 0 ? true : false;

This would be true, you can also do direct manipulation within the braces as well as return values as well, which is what your line of code is doing:

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

is the same as:

if(isset($contents[$item]))
{
    $contents[$item] = $contents[$item] + 1;
}else
{
     $contents[$item] = 1;
}

The ternary operators is available across many languages, see Wiki

Resources:

  • Ternary Operators in PHP
  • Ternary Operator's in general
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜