0

How can I simplify this:

if($quantity == 0)
   $basket_item['status_text'] = 'out of stock';

else if($quantity >= $requested)
   $basket_item['status_text'] = 'available';

else if($quantity < $requested)
   $basket_item['status_text'] = 'not enough in stock';

As you can see I have to keep defining the $basket_item['status_text'] variable each time.

EDIT: just fixed the code

2 Answers 2

1

You can make function as below

function defineStatus($quantity,$requested) {
   if($quantity == 0)
     return 'out of stock';

   else if($quantity >= $requested)
     return 'available';

   else if($quantity < $requested)
     return 'not enough in stock';
}

Now Call function where you want to take this status decision.

$basket_item['status_text'] = defineStatus($quantity,$requested);
Sign up to request clarification or add additional context in comments.

2 Comments

@MartijnPieters I didn't realize it. I will take care next time
Please do; edit reviewers are supposed to pay attention to what the edit changes. Blatant spam like that should never be approved.
1

Yes, you can.

Just do it: if($quantity == 0 || $quantity < $requested)

Your code will look like:

if($quantity == 0 || $quantity < $requested)
   $status_text = 'not in stock';
   // $basket_item['status_text'] = 'not in stock';

else if($quantity >= $requested)
   $status_text = 'available';

I've commented a line, that's because i guess you're using only $status_text variable, but not using the another.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.