Ryokotsusai Posted September 9, 2007 Share Posted September 9, 2007 I see things used in a lot of scripts: if(!isset(this)) {do this} else {or do this} and my question is, Is using not set, or not equal to better, or does it have some advantage over using just isset() or ==? 'cause from what i have seen everyone seems to go towards "is not..." Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/ Share on other sites More sharing options...
TheFilmGod Posted September 9, 2007 Share Posted September 9, 2007 I dont understand why it matters. It depends on the situation. Sometimes a variable will be given different values depending on parameters and sometimes is only set if a form was submitted. So there is no advantage in using anyone of those. THERE is an advantage in using it when you need to. common sense. I'm a noob. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344564 Share on other sites More sharing options...
Fadion Posted September 9, 2007 Share Posted September 9, 2007 U can use isset() to be sure a variable is not null or not unset()-ed before, while u use the equality operator == to compare that variable to a defined value. They are different methods and provide different handling of situations. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344568 Share on other sites More sharing options...
Ryokotsusai Posted September 9, 2007 Author Share Posted September 9, 2007 isset wasn't the question i was talking about th preference ppl seem to have of using "!" in if statements @filmgod i agree that in a lot of situations it does make sense, but then again in just as many there is an "else" statment, that was always a part of it, which negates the need for the "!" as it does something anyway and would work the same the other way ie: if($this != $that) {echo "They are not the same."; } else {echo "They are the same.";} does the same thing as: if($this == $that) {echo "They are the same.";} else {echo "They are not the same.";} right? yet out of all the examples i have read, post i have seen, and scripts I have torn apart, 90% of the time the person who wrote it used != or !isset() with an else. So if it has no real advantage in a situation where there is an 'else' are we just drawn toward a negative? Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344580 Share on other sites More sharing options...
TheFilmGod Posted September 9, 2007 Share Posted September 9, 2007 isset wasn't the question i was talking about th preference ppl seem to have of using "!" in if statements @filmgod i agree that in a lot of situations it does make sense, but then again in just as many there is an "else" statment, that was always a part of it, which negates the need for the "!" as it does something anyway and would work the same the other way ie: if($this != $that) {echo "They are not the same."; } else {echo "They are the same.";} does the same thing as: if($this == $that) {echo "They are the same.";} else {echo "They are not the same.";} right? yet out of all the examples i have read, post i have seen, and scripts I have torn apart, 90% of the time the person who wrote it used != or !isset() with an else. So if it has no real advantage in a situation where there is an 'else' are we just drawn toward a negative? YOu need to code php in such a way that the less common action will be the if. You should code, if (form was submitted) { then do this }else { do this} Not the other way around for various reasons. Let's say the code somehow got messed up or something, then the default (else) would be exectuted and thus should be the default or more common action. Awesome thefilmgod example: if ($authorized != true) { do not give authorizaton } else { give authorization } Althought it should always work the else statement to give authorization as the last end option is pretty stupid logically. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344587 Share on other sites More sharing options...
Fadion Posted September 9, 2007 Share Posted September 9, 2007 The NOT (!) operator will give u more control if the situation requests it and most of the time make your code a bit cleaner. For example in a boolean variable: instead of "if($var == false)" u use "if(!$var)". It all depends of the situation and how u're used to coding. I have a practice of using it a lot of time and i like that way. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344595 Share on other sites More sharing options...
d22552000 Posted September 9, 2007 Share Posted September 9, 2007 hmm I think this is what hes asking and I would like to know the anser... in the following, which is the best? fastest? mose secure? if (A!=1) if (!A=1) if (A is not 1) if (A does not equal 1) Cause on my servery they all work, and my tries to time them were completely random. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344626 Share on other sites More sharing options...
d22552000 Posted September 9, 2007 Share Posted September 9, 2007 so whats hte answer? Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344945 Share on other sites More sharing options...
Jessica Posted September 9, 2007 Share Posted September 9, 2007 Uhm, only one of them is valid code. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-344947 Share on other sites More sharing options...
prodigy2k7 Posted September 10, 2007 Share Posted September 10, 2007 well how I like to code is have whichever way will have the MAIN stuff at the top and the error trapping below. IE: (Only wanting prodigy2k7 to visit) if ($name == prodigy2k7) { do this; also this; and this; and this; more of this; and this; } else { echo "You are not allowed." } OR (Wanting anyone EXCEPT prodigy2k7 to visit) if ($name != prodigy2k7) { do this; also this; and this; and this; more of this; and this; } else { echo "You are not allowed." } See what I mean? It really depends on what your doing. I like to have most of the code at the top, so therefore I do whatever is necesarry at the top using == or != so it really depends on the person and what your doing. No there is no difference. It is a language that you are coding for yourself. Look at Shakespeare, he writes how he wants to. You can code how you want to, how you best understand it. Enjoy Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-345037 Share on other sites More sharing options...
d22552000 Posted September 11, 2007 Share Posted September 11, 2007 lol I even made a function for echo cause im bored of typing the word echo... function a($a) { echo $a; } LOL!!! A("im bored of echo"); Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-346431 Share on other sites More sharing options...
corbin Posted September 11, 2007 Share Posted September 11, 2007 If you have notices turned on with php: if($var == "hi") If you do that, and var hasn't been initialialized, then PHP will throw a warning (which most people hide). As for if(!condition).... I think that's just personal preference.... Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-346434 Share on other sites More sharing options...
d22552000 Posted September 14, 2007 Share Posted September 14, 2007 no ti wont, depends on if you ahve ~warnings. and it also depends on if you have automatic variables on. Quote Link to comment https://forums.phpfreaks.com/topic/68544-randomish-question-on-ifs/#findComment-348695 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.