There is an HTML page from which fruit data is sent to the server using AJAX using the POST method.

On the PHP side, I accept this data:

$banana = $_POST['banana']; // 2 штуки $apple = $_POST['apple']; // 5 штук $orange = $_POST['orange']; // 3 штуки 

Depending on the availability of variables, I have to output the text:

 if (isset($banana)) { echo '2 банана'; } if (isset($banana) && isset($apple)) { echo '2 банана и 5 яблок'; } if (isset($banana) && isset($apple) && isset($orange)) { echo '2 банана, 5 яблок и 3 апельсина'; } 

The problem is that the presence of all three variables in turn displays all 3 conditions, instead of the one you need:

'2 bananas2 bananas and 5 apples2 bananas, 5 apples and 3 oranges'.

It is clear why: all 3 variables exist.

I need that if all 3 variables are present at the same time, only one condition is output, and not three at once, that is, it should be:

  echo '2 банана, 5 яблок и 3 апельсина'; 

If at the same time there are 2 variables, there is also one condition, without a superior, namely:

  echo '2 банана и 5 яблок'; 

How can this be implemented?

  • Solutions are all working, everyone is equally good ... just the same ... but everyone has a drawback - they do not scale well. Hardcode. And if there are 4 fruits, will you run the code like this? Five? Six? Nobody sounded a good option (though not the fact that it is needed by the top-starter) when the string "X something, Y another, ..... and Z last" is formed based on the analysis of the array, and then it is output. - AK
  • @AK, it is, of course, but asked specifically - they answered the same. We can not think out - and here we start using templates, so we’ll pull Twig , hmm, and the array can still be over GET , transferred to different routes, we will pull up the router from the Symfony package (and for every fireman’s package). - user207618
  • @Other ... and reject nouns depending on the quantity. Well, yes, so I just grumbled a bit in the comments and that's it. Let it remain a hint for the future of top-starters or who will read the question. - AK
  • @AK, grumble in the comments ... What could be better? :) - user207618

2 answers 2

Use elseif condition.

Important : It is necessary to check from the most specific requests to the most general (from combinations of three, to checking one):

 $banana = true;//$_POST['banana']; // 2 штуки $apple = true;//$_POST['apple']; // 5 штук $orange = true;//$_POST['orange']; // 3 штуки if(isset($banana) && isset($apple) && isset($orange)){ echo '2 банана, 5 яблок и 3 апельсина'; }elseif(isset($banana) && isset($apple)){ echo '2 банана и 5 яблок'; }elseif(isset($banana)){ echo '2 банана'; } 

https://repl.it/Calu/0

    Apparently, you need to rearrange them and use elseif :

     if (isset($banana) && isset($apple) && isset($orange)) { echo '2 банана, 5 яблок и 3 апельсина'; } elseif (isset($banana) && isset($apple)) { echo '2 банана и 5 яблок'; } elseif (isset($banana)) { echo '2 банана'; } 

    that subsequent conditions are checked only if the previous ones are incorrect.