Bug in LuckyNumbers

this is task 3: Write a function validate that accepts the user input as a parameter. If the user did not provide any input, validate should return 'Required field' . If the input does not represent a positive non-zero whole number (according to the [PHP type casting rules], 'Must be a whole number larger than 0' should be returned. In all other cases you can assume the input is valid, the return value should be an empty string then.
My code is working for all cases except one where it should return an empty string for a valid input but its returning “must be a whole number larger than 0” instead. This is my code:
if ($input == “”){
return “Required field”;
else if(!is_numeric($input) || intval($input) <= 0 || (intval($input)) != $input){
return “Must be a whole number larger than 0”;
else return “”;
Any help is appreciated.

@anoushka20 I think the problem is that the elseif is checking too many conditions.

If you look at the test cases in LuckyNumbersTest.php, there is this value where your code is failing:


Try removing some of those conditions.

Hey! Thank you for replying. I did try removing the first condition as it is redundant but the same testcase is failing again. Also, how can i check the input for which the testcase is failing? I can’t seem to find it in the failed testcase.

If you have the code locally, the value is at line 149 of LuckyNumbersTest.php. I always download exercises, so I don’t know if you get to see the test code if you use the online editor.

Anyway, you’ll have to work out which check is necessary to get the tests to pass. The only hint I can give you is that only one check is needed in the else if.

@anoushka20 I apologize for the bad test feedback. You cannot see the failing input in the online editor. We will work on that.

I also found, that debugging using echo or var_dump() currently doesn’t help in this exercise. This will be fixed, too.

@rzuckerm showed you the failing value 00015-plus. Maybe you can read the hints on task 3 in the “Get help” tab of the editor (the very last tab on the left, there is a scroll bar if you don’t see it directly). They say, that automatic type conversion (which happens with == and !=) is doing the wrong thing here.


Alright I’ll take a look at it again. Thanks!
Edit: I just understood where I was making the mistake and have fixed it now. Thank you so much!

I’ll try that as well. Thanks for the help!

This will fix the user output when merged: