I have the code above.
There are 6 pieces of pizza in the code above.
let’s suppose that I don’t know how many pieces of pizza are there.
How can I know the number of the pieces of pizza?
The code below work fine but it is, you think, foolish.
Expanding on the solution from @m_hutley above, you also need to take into account that you will find errors when running your original code, because you are be checking to see whether a non-existent array element has a certain value without checking whether or not that element exists. You’d get an undefined offset in a numerical array like this, or an undefined index in an associative array.
So in addition, have a look at isset, which can be wrapped around any reference to a variable that might not exist.