Reputation: 11
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
Upvotes: 1
Views: 2061
Reputation: 145492
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
is functionally equivalent to:
@$contents[$item]++;
(I'm looking forward to your corrections.)
Upvotes: 2
Reputation: 57268
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:
Upvotes: 0
Reputation: 724252
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
Upvotes: 5
Reputation: 1349
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;
}
Upvotes: 0
Reputation: 6700
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.
Upvotes: 0
Reputation: 8040
It's just a fancy way of writing
if (isset($contents[$item]))
$contents[$item] = $contents[$item] + 1;
else
$contents[$item] = 1;
Upvotes: 1