quelle Posted April 19, 2011 Share Posted April 19, 2011 Actually what's the difference between if(...) and if(isset(...) Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/ Share on other sites More sharing options...
Maq Posted April 19, 2011 Share Posted April 19, 2011 isset - Determine if a variable is set and is not NULL. if - If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More detailed explanation in the manual links above. Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1203701 Share on other sites More sharing options...
quelle Posted April 19, 2011 Author Share Posted April 19, 2011 what actually does it mean NULL and look at this, for example i could take a variable from a form on both ways if($_POST['name']) { is same like this ? if(isset($_POST['name'])) { Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1203717 Share on other sites More sharing options...
spiderwell Posted April 19, 2011 Share Posted April 19, 2011 the first example will throw an error saying name is undefined if it wasnt found in the $_POST array, that is Null isset is a way of being sure something exists before retreiving its value i usually retreive all my form values like this if (isset($_POST['name'])) $name = $_POST['name']; Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1203723 Share on other sites More sharing options...
quelle Posted April 20, 2011 Author Share Posted April 20, 2011 but what if $_POST['name'] actually exists, whats the difference then ? if($_POST... would have returned the same as isset Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1204089 Share on other sites More sharing options...
spiderwell Posted April 20, 2011 Share Posted April 20, 2011 yes you would get the same thing at the end. its a technique to prevent errors in your code with undefined variables. its down to personal preference, but really its better to catch potential errors than not, even if you know that $_POST is going to be there. you dont have to do it, just like you dont have to do many things in code, but in a professional environment, it would be expected to write code that is covering any possible errors that might be thrown. Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1204107 Share on other sites More sharing options...
maxudaskin Posted April 20, 2011 Share Posted April 20, 2011 if is not a function. It's merely an operator (correct me if I'm using the wrong terms). isset is a function. It checks if the variable that you are looking for actually exists in memory and returns true if it is, false if it isn't. empty is a function. It checks if the variable is in memory and if it's not NULL, '' or whatever it will juggle as an empty variable. It returns false if one of those conditions are false. I suggest that you do some reading of these PHP tutorials on W3Schools. Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1204214 Share on other sites More sharing options...
spiderwell Posted April 21, 2011 Share Posted April 21, 2011 only dont go to w3schools as they are not at all associated with w3c and are often giving out incorrect information. but thats a whole different story altogether Quote Link to comment https://forums.phpfreaks.com/topic/234188-difference-btw-these-2/#findComment-1204385 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.