Jump to content

[SOLVED] Best way to check for a variable?


davidz

Recommended Posts

When checking whether or not a variable is set which method is better coding practice?

if ($_SESSION['value1']){
//Do some code
}
else {
//Do something else
}

OR

if (isset($_SESSION['value1'])){
//Do some code
}
else {
//Do something else
}

They both work just fine, the first one gives a warning error.

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/41971-solved-best-way-to-check-for-a-variable/
Share on other sites

The problem with the first method is, that if that variable holds boolean false, 0, an empty array, empty string, a string that holds 0 or NULL (I hope I didn't skip anything) the condition will be false and then it won't work. Using isset() is much better.

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.