While building a small Feedback-Solution where people can give a number of stars (between 0 and 5), I noticed that all user submitted ratings are stored with just 1 star.
I tried it myself by submitting 5 stars and the backend still shows 1 star.
So I looked into the code and this is the piece that causes the trouble:
$feedback->rating = ($wire->input->post->rating || 1);
|| operator isn't doing what I suspected it to do.
In fact it just returns 1 every time (unless both hand sides are $false).
Check my example code below:
$example1 = ($true || 5);
$example2 = ($false || 5);
$example3 = ($false || $false);
$example4 = (5 || 0);
Also I made a paste here: https://eval.in/514978.
What I'm assuming is, PHP tries to convert the statements to an integer (either 0 or 1) depending on the given elements, is that true?
I'm used to use the
var i = myFunction() || "default";
This will check if myFunction() returns a bool-ish value and if not just uses the right hand side value (rather than turning everything into an int).