Can this PHP statement with a isset() check be simplified?
Needing to check whether the item exists in the array and either add to it or increase the value by the amount.
if(isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] += $amount;
}else{
$this->_costRemovedByLineItem[$objectId] = $amount;
}
I have a feeling this can be simplified.
php
add a comment |
Needing to check whether the item exists in the array and either add to it or increase the value by the amount.
if(isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] += $amount;
}else{
$this->_costRemovedByLineItem[$objectId] = $amount;
}
I have a feeling this can be simplified.
php
What version of PHP?
– Nick
3 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago
add a comment |
Needing to check whether the item exists in the array and either add to it or increase the value by the amount.
if(isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] += $amount;
}else{
$this->_costRemovedByLineItem[$objectId] = $amount;
}
I have a feeling this can be simplified.
php
Needing to check whether the item exists in the array and either add to it or increase the value by the amount.
if(isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] += $amount;
}else{
$this->_costRemovedByLineItem[$objectId] = $amount;
}
I have a feeling this can be simplified.
php
php
asked 4 hours ago
lukemhlukemh
2,15762135
2,15762135
What version of PHP?
– Nick
3 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago
add a comment |
What version of PHP?
– Nick
3 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago
What version of PHP?
– Nick
3 hours ago
What version of PHP?
– Nick
3 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago
add a comment |
3 Answers
3
active
oldest
votes
This method reduces it a bit, as you don't need the else statement or to assign amount twice.
if(!isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] = 0;
}
$this->_costRemovedByLineItem[$objectId] += $amount;
add a comment |
Something that will work in all versions of PHP:
@$this->_costRemovedByLineItem[$objectId] += $amount;
The @
(error control operator) will cause PHP to ignore the error caused by the non-existent index, create the element (with an empty value) and then add $amount
to it, resulting in a value of $amount
(as the empty value gets converted to 0 as a number).
Warning use of the
@
operator could potentially make it harder to debug
your code, as it will hide error messages that you may have
needed to see (for example, even if$this
doesn't exist, or there is
no object element called_costRemovedByLineItem
, PHP will create
them along with the array). See the third case in my example code.
Alternatively in PHP7 you can use the Null Coalescing Operator
:
$this->_costRemovedByLineItem[$objectId] = ($this->_costRemovedByLineItem[$objectId] ?? 0) + $amount;
And in PHP < 7 you can use the ternary operator
$this->_costRemovedByLineItem[$objectId] = (isset($this->_costRemovedByLineItem[$objectId]) ? $this->_costRemovedByLineItem[$objectId] : 0) + $amount;
A shorter example of each:
$amount = 4;
@$a[5] += $amount;
print_r($a);
$b[6] = ($b[6] ?? 0) + $amount;
print_r($b);
@$c->x[5] += $amount;
print_r($c);
$d[3] = (isset($d[3]) ? $d[3] : 0) + $amount;
print_r($d);
Output:
Array ( [5] => 4 )
Array ( [6] => 4 )
stdClass Object (
[x] => Array ( [5] => 4 )
)
Demo on 3v4l.org
Now that is nice
– Siavas
3 hours ago
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX
– Devon
3 hours ago
@Devon the failure in your demo occurs when_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.
– Nick
2 hours ago
add a comment |
Ternary operators!
$this->_costRemovedByLineItem[$objectId] =
(isset($this->_costRemovedByLineItem[$objectId])) ?
$this->_costRemovedByLineItem[$objectId] + $amount :
$amount;
One sec... is this actually simpler?
But maybe...
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?? 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
This is for PHP 7+ only though. Or if you don't mind E_NOTICEs, from PHP 5.3:
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?: 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
One sec... this we just make the code harder to understand with not much improved?
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54432376%2fcan-this-php-statement-with-a-isset-check-be-simplified%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This method reduces it a bit, as you don't need the else statement or to assign amount twice.
if(!isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] = 0;
}
$this->_costRemovedByLineItem[$objectId] += $amount;
add a comment |
This method reduces it a bit, as you don't need the else statement or to assign amount twice.
if(!isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] = 0;
}
$this->_costRemovedByLineItem[$objectId] += $amount;
add a comment |
This method reduces it a bit, as you don't need the else statement or to assign amount twice.
if(!isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] = 0;
}
$this->_costRemovedByLineItem[$objectId] += $amount;
This method reduces it a bit, as you don't need the else statement or to assign amount twice.
if(!isset($this->_costRemovedByLineItem[$objectId])) {
$this->_costRemovedByLineItem[$objectId] = 0;
}
$this->_costRemovedByLineItem[$objectId] += $amount;
answered 3 hours ago
DevonDevon
22.8k42746
22.8k42746
add a comment |
add a comment |
Something that will work in all versions of PHP:
@$this->_costRemovedByLineItem[$objectId] += $amount;
The @
(error control operator) will cause PHP to ignore the error caused by the non-existent index, create the element (with an empty value) and then add $amount
to it, resulting in a value of $amount
(as the empty value gets converted to 0 as a number).
Warning use of the
@
operator could potentially make it harder to debug
your code, as it will hide error messages that you may have
needed to see (for example, even if$this
doesn't exist, or there is
no object element called_costRemovedByLineItem
, PHP will create
them along with the array). See the third case in my example code.
Alternatively in PHP7 you can use the Null Coalescing Operator
:
$this->_costRemovedByLineItem[$objectId] = ($this->_costRemovedByLineItem[$objectId] ?? 0) + $amount;
And in PHP < 7 you can use the ternary operator
$this->_costRemovedByLineItem[$objectId] = (isset($this->_costRemovedByLineItem[$objectId]) ? $this->_costRemovedByLineItem[$objectId] : 0) + $amount;
A shorter example of each:
$amount = 4;
@$a[5] += $amount;
print_r($a);
$b[6] = ($b[6] ?? 0) + $amount;
print_r($b);
@$c->x[5] += $amount;
print_r($c);
$d[3] = (isset($d[3]) ? $d[3] : 0) + $amount;
print_r($d);
Output:
Array ( [5] => 4 )
Array ( [6] => 4 )
stdClass Object (
[x] => Array ( [5] => 4 )
)
Demo on 3v4l.org
Now that is nice
– Siavas
3 hours ago
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX
– Devon
3 hours ago
@Devon the failure in your demo occurs when_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.
– Nick
2 hours ago
add a comment |
Something that will work in all versions of PHP:
@$this->_costRemovedByLineItem[$objectId] += $amount;
The @
(error control operator) will cause PHP to ignore the error caused by the non-existent index, create the element (with an empty value) and then add $amount
to it, resulting in a value of $amount
(as the empty value gets converted to 0 as a number).
Warning use of the
@
operator could potentially make it harder to debug
your code, as it will hide error messages that you may have
needed to see (for example, even if$this
doesn't exist, or there is
no object element called_costRemovedByLineItem
, PHP will create
them along with the array). See the third case in my example code.
Alternatively in PHP7 you can use the Null Coalescing Operator
:
$this->_costRemovedByLineItem[$objectId] = ($this->_costRemovedByLineItem[$objectId] ?? 0) + $amount;
And in PHP < 7 you can use the ternary operator
$this->_costRemovedByLineItem[$objectId] = (isset($this->_costRemovedByLineItem[$objectId]) ? $this->_costRemovedByLineItem[$objectId] : 0) + $amount;
A shorter example of each:
$amount = 4;
@$a[5] += $amount;
print_r($a);
$b[6] = ($b[6] ?? 0) + $amount;
print_r($b);
@$c->x[5] += $amount;
print_r($c);
$d[3] = (isset($d[3]) ? $d[3] : 0) + $amount;
print_r($d);
Output:
Array ( [5] => 4 )
Array ( [6] => 4 )
stdClass Object (
[x] => Array ( [5] => 4 )
)
Demo on 3v4l.org
Now that is nice
– Siavas
3 hours ago
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX
– Devon
3 hours ago
@Devon the failure in your demo occurs when_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.
– Nick
2 hours ago
add a comment |
Something that will work in all versions of PHP:
@$this->_costRemovedByLineItem[$objectId] += $amount;
The @
(error control operator) will cause PHP to ignore the error caused by the non-existent index, create the element (with an empty value) and then add $amount
to it, resulting in a value of $amount
(as the empty value gets converted to 0 as a number).
Warning use of the
@
operator could potentially make it harder to debug
your code, as it will hide error messages that you may have
needed to see (for example, even if$this
doesn't exist, or there is
no object element called_costRemovedByLineItem
, PHP will create
them along with the array). See the third case in my example code.
Alternatively in PHP7 you can use the Null Coalescing Operator
:
$this->_costRemovedByLineItem[$objectId] = ($this->_costRemovedByLineItem[$objectId] ?? 0) + $amount;
And in PHP < 7 you can use the ternary operator
$this->_costRemovedByLineItem[$objectId] = (isset($this->_costRemovedByLineItem[$objectId]) ? $this->_costRemovedByLineItem[$objectId] : 0) + $amount;
A shorter example of each:
$amount = 4;
@$a[5] += $amount;
print_r($a);
$b[6] = ($b[6] ?? 0) + $amount;
print_r($b);
@$c->x[5] += $amount;
print_r($c);
$d[3] = (isset($d[3]) ? $d[3] : 0) + $amount;
print_r($d);
Output:
Array ( [5] => 4 )
Array ( [6] => 4 )
stdClass Object (
[x] => Array ( [5] => 4 )
)
Demo on 3v4l.org
Something that will work in all versions of PHP:
@$this->_costRemovedByLineItem[$objectId] += $amount;
The @
(error control operator) will cause PHP to ignore the error caused by the non-existent index, create the element (with an empty value) and then add $amount
to it, resulting in a value of $amount
(as the empty value gets converted to 0 as a number).
Warning use of the
@
operator could potentially make it harder to debug
your code, as it will hide error messages that you may have
needed to see (for example, even if$this
doesn't exist, or there is
no object element called_costRemovedByLineItem
, PHP will create
them along with the array). See the third case in my example code.
Alternatively in PHP7 you can use the Null Coalescing Operator
:
$this->_costRemovedByLineItem[$objectId] = ($this->_costRemovedByLineItem[$objectId] ?? 0) + $amount;
And in PHP < 7 you can use the ternary operator
$this->_costRemovedByLineItem[$objectId] = (isset($this->_costRemovedByLineItem[$objectId]) ? $this->_costRemovedByLineItem[$objectId] : 0) + $amount;
A shorter example of each:
$amount = 4;
@$a[5] += $amount;
print_r($a);
$b[6] = ($b[6] ?? 0) + $amount;
print_r($b);
@$c->x[5] += $amount;
print_r($c);
$d[3] = (isset($d[3]) ? $d[3] : 0) + $amount;
print_r($d);
Output:
Array ( [5] => 4 )
Array ( [6] => 4 )
stdClass Object (
[x] => Array ( [5] => 4 )
)
Demo on 3v4l.org
edited 2 hours ago
answered 3 hours ago
NickNick
27.3k111941
27.3k111941
Now that is nice
– Siavas
3 hours ago
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX
– Devon
3 hours ago
@Devon the failure in your demo occurs when_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.
– Nick
2 hours ago
add a comment |
Now that is nice
– Siavas
3 hours ago
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX
– Devon
3 hours ago
@Devon the failure in your demo occurs when_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.
– Nick
2 hours ago
Now that is nice
– Siavas
3 hours ago
Now that is nice
– Siavas
3 hours ago
1
1
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using
@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX– Devon
3 hours ago
The null coalescing example is great imho, but the first example can be really problematic when debugging. I'd never recommend using
@
. What if _costRemovedByLineItem is deleted, or is an object. The code will fail without any error in anything under 7.1: 3v4l.org/pvvrX– Devon
3 hours ago
@Devon the failure in your demo occurs when
_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.– Nick
2 hours ago
@Devon the failure in your demo occurs when
_costRemovedByLineItem
is an object, but if it is OPs code will also fail as they are trying to use it as an array too.– Nick
2 hours ago
add a comment |
Ternary operators!
$this->_costRemovedByLineItem[$objectId] =
(isset($this->_costRemovedByLineItem[$objectId])) ?
$this->_costRemovedByLineItem[$objectId] + $amount :
$amount;
One sec... is this actually simpler?
But maybe...
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?? 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
This is for PHP 7+ only though. Or if you don't mind E_NOTICEs, from PHP 5.3:
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?: 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
One sec... this we just make the code harder to understand with not much improved?
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
add a comment |
Ternary operators!
$this->_costRemovedByLineItem[$objectId] =
(isset($this->_costRemovedByLineItem[$objectId])) ?
$this->_costRemovedByLineItem[$objectId] + $amount :
$amount;
One sec... is this actually simpler?
But maybe...
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?? 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
This is for PHP 7+ only though. Or if you don't mind E_NOTICEs, from PHP 5.3:
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?: 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
One sec... this we just make the code harder to understand with not much improved?
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
add a comment |
Ternary operators!
$this->_costRemovedByLineItem[$objectId] =
(isset($this->_costRemovedByLineItem[$objectId])) ?
$this->_costRemovedByLineItem[$objectId] + $amount :
$amount;
One sec... is this actually simpler?
But maybe...
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?? 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
This is for PHP 7+ only though. Or if you don't mind E_NOTICEs, from PHP 5.3:
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?: 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
One sec... this we just make the code harder to understand with not much improved?
Ternary operators!
$this->_costRemovedByLineItem[$objectId] =
(isset($this->_costRemovedByLineItem[$objectId])) ?
$this->_costRemovedByLineItem[$objectId] + $amount :
$amount;
One sec... is this actually simpler?
But maybe...
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?? 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
This is for PHP 7+ only though. Or if you don't mind E_NOTICEs, from PHP 5.3:
$this->_costRemovedByLineItem[$objectId] = $this->_costRemovedByLineItem[$objectId] ?: 0;
$this->_costRemovedByLineItem[$objectId] += $amount;
One sec... this we just make the code harder to understand with not much improved?
answered 3 hours ago
SiavasSiavas
1,7161922
1,7161922
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
add a comment |
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yeah, I agree all the solutions are a little shorter, but I am not sure they improve readability much.
– lukemh
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
Yep, this is what I was trying to say :)
– Siavas
3 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54432376%2fcan-this-php-statement-with-a-isset-check-be-simplified%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What version of PHP?
– Nick
3 hours ago
Instead of trying to "simplify", go for code readability, you'll thank yourself later. Imho also go ahead and add a check for if is that property an array or not and add a handling for that scenario, cause php ain't letting you set an index for a boolean, now is it. 1 more vector of error squashed.
– Mohd Abdul Mujib
2 hours ago