DEV Community

Discussion on: Can you find the bug in this piece of php code? ๐Ÿคน

Collapse
 
nombrekeff profile image
Keff • Edited

Nice stuff, now that's how you do it!! Thanks for the detailed explanation

Collapse
 
rkallan profile image
RRKallan • Edited

@nombrekeff thx

I see only one missing explanation

In my example i use !empty($pin) and not isset($pin)
Explanation as code

$x = " ";
$trimX = trim($x); // $trimX = ""

var_dump(isset($trimX)); // return true
var_dump(!empty($trimX)); // return false
Enter fullscreen mode Exit fullscreen mode

as we can see from the output it would be wise to use empty() in this case

BTW i'm very curious about your solution :)

Thread Thread
 
nombrekeff profile image
Keff

Ohh nice point, I did not know that (I'm not a php dev), so it's really helpfull. I guess isset only checks whether the variable exists right? And empty checks if the length > 0, have I gotten that right?

My solution is quite similar to yours actually, I just make sure the input is what I expect, and I don't trust user input whatsoever.

Thread Thread
 
rkallan profile image
RRKallan • Edited

I would not define my self as PHP developer. My cup of thea is HTML(5), (S)CSS and Javascript :p

I guess isset only checks whether the variable exists right?

Yes isset checks if variable is defined.
If i'm right $x = undefined is not possible and needs to be null when no value is assigned

empty checks if the length > 0

correct if you mean !empty() > 0 is not a empty string if character length > 0, better said empty() returns true as boolean when character length = 0. NOTE a space is a character

Thread Thread
 
nombrekeff profile image
Keff

Fantastic, thanks again! I might need to refresh my php knowledge