I’m sometimes confused to using which one of them,
say I have a function called getmember($id)
function getmember($id)
{
// now this is the confusing part
// how do i test if a $id was set or not set?
//solution 1
if(empty($id))
{
return false;
}
// solution 2
if(isset($id))
{
return false;
}
}
That’s sometimes not clear to me, sometimes if a parameter in a function is set like function($var="")
Then I do
if($var ==="")
{
return false;
}
What should I use the next time isset ? empty ? or ===''?
Here you go, a complete breakdown of what works and when:
THE OUTPUT:
When I show
(bool)$variableabove, that is how you could use it in a conditional. For example, to check if a variable is null or empty, you could do:But it’s best to use a function since it’s a little more readable. But it’s your choice.
Also, check the PHP type comparison table. It’s basically what I just did above, except much more.