davidz Posted March 9, 2007 Share Posted March 9, 2007 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!! Quote Link to comment https://forums.phpfreaks.com/topic/41971-solved-best-way-to-check-for-a-variable/ Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 The second is better, it is good practice to always check to see if there is a value when pulling GET POST or SESSION data. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/41971-solved-best-way-to-check-for-a-variable/#findComment-203660 Share on other sites More sharing options...
Orio Posted March 9, 2007 Share Posted March 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/41971-solved-best-way-to-check-for-a-variable/#findComment-203665 Share on other sites More sharing options...
davidz Posted March 13, 2007 Author Share Posted March 13, 2007 Thanks for the input!! Quote Link to comment https://forums.phpfreaks.com/topic/41971-solved-best-way-to-check-for-a-variable/#findComment-206571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.