Reputation: 5758
i have this variable.
$productId = 2; // Testing ID
$value->id; // Contains INT
$value->datePurchaseEnd; // Contains UNIXTIME or NULL
The conditional i want to make.
if ($value->id == $productId) return true;
but if $value->datePurchaseEnd;
is not NULL
then also compare it with current time and it must bigger than current time to return TRUE
;
For now this is the code i made:
if( $value->id == $productId){
if( $value->datePurchaseEnd == NULL ){
$return = TRUE; break;
}else{
if( $value->datePurchaseEnd > mktime() ){
$return = TRUE; break;
}
}
}
But i feel this code is not good.
Is there any suggestion to make better code with conditional above?
Upvotes: 0
Views: 120
Reputation: 12737
If your code is not in a loop, I do prefer the inline conditional:
return $value->id == $productId && ($value->datePurchaseEnd == null || $value->datePurchaseEnd > mktime())
Upvotes: 0
Reputation: 1575
if( $value->id == $productId){
$return = ($value->datePurchaseEnd == NULL || $value->datePurchaseEnd > mktime());
break;
}
or
$return = $value->id == $productId && ($value->datePurchaseEnd == NULL || $value->datePurchaseEnd > mktime());
break;
Upvotes: 2
Reputation: 4459
I would say either
if( ($value->id == $productId) && ($value->datePurchaseEnd == NULL || $value->datePurchaseEnd > mktime() )) {
$return = true;
break;
}
or
if($value->id == $productId) {
if($value->datePurchaseEnd == NULL || $value->datePurchaseEnd > mktime()) {
$return = true;
break;
}
}
Depends on if the conditions need an else
Upvotes: 6