zerotolerance Posted March 29, 2012 Share Posted March 29, 2012 Hi , I'm new on this forum so don't judge too hard.. But I have a few questions. I am recently new to PHP as this is my first computer language I've learnt besides HTML. My first question is, what's the difference between !== and !=, and is it more secure to use !== when comparing two fields such as passwords? I've different things and this has confused me.. And my second question is, I have this piece of code /* option error checking */ $field = "option"; // Use field name for option /* Check if the option picked is from the list of options to choose from*/ if($suboption !== "option1" || $suboption !== "option2" || $suboption !== "option3" || $suboption !== "option4" || $suboption !== "option5" || $suboption !== "option6" || $suboption !== "option7"){ $form->setError($field, "* Unexpected error with option"); } I'm guessing in the long run, this will slow down my script.. is there an easier way of checking without using || $suboption !== and is it okay if I've used !==? Like I said I'm new to this so please don't flame.. I've done some research but I can't seem to find some accurate answer. Thank you, ZT Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/ Share on other sites More sharing options...
rythemton Posted March 29, 2012 Share Posted March 29, 2012 To understand the difference between != and !==, you need to understand the difference between == and ===. The triple = will verify type as well as value, so it is stricter: if( "24" == 24 ) // This will return TRUE if( "24" === 24 ) // This will return FALSE because one is an INT, the other a STRING if( 1 == TRUE ) // This will return TRUE if( 1 === TRUE ) // This will return FALSE because one is an INT, the other a BOOLEAN if( FALSE == "" ) // This will return TRUE if( FALSE === "" ) // This will return FALSE because one is a BOOLEAN, the other a STRING != is the opposite of ==, so if == is TRUE, then != is FALSE. !== is the opposite of ===, so if === is TRUE, then !== is FALSE. if( "24" != 24 ) // This will return FALSE if( "24" !== 24 ) // This will return TRUE because one is an INT, the other a STRING if( 1 != TRUE ) // This will return FALSE if( 1 !== TRUE ) // This will return TRUE because one is an INT, the other a BOOLEAN if( FALSE != "" ) // This will return FALSE if( FALSE !== "" ) // This will return TRUE because one is a BOOLEAN, the other a STRING Sometimes you should use one over the other, and sometimes it don't matter. It will take experience to learn when it matters and when it doesn't. I hope this helps. Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1332221 Share on other sites More sharing options...
BK87 Posted March 29, 2012 Share Posted March 29, 2012 <?php $value_one=26; //integer $value_two="26"; //string if($value_one == $value_two){ echo "the values are identical"; //they are both `26` --- string or integer } /* * this next example featuring "===" (explicit) will never process because $value_one is an integer and $value_two is a string, yet they have the same value of `26` * comparing two strings with explicit isn't necessary * good practice is to compare TRUE and FALSE statements this way. * to make sure that "true" (string) isn't the same as TRUE (boolean) */ if($value_one === $value_two){ echo "the values are identical"; } ?> i hope I got that right. Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1332222 Share on other sites More sharing options...
rythemton Posted March 29, 2012 Share Posted March 29, 2012 /* to make sure that "true" (string) isn't the same as TRUE (boolean) */ PHP considers every non-empty string to be TRUE, except "0", when comparing a STRING to a BOOLEAN. if( "FALSE" == TRUE ) // This will be considered TRUE because the string is not empty. if( "FALSE" === TRUE ) // This will be considered FALSE because one is a STRING and the other a BOOLEAN Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1332223 Share on other sites More sharing options...
BK87 Posted March 29, 2012 Share Posted March 29, 2012 hmm now that I think about it I don't even know why I wrote that... idiot me, thanks for correcting me. Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1332225 Share on other sites More sharing options...
zerotolerance Posted April 1, 2012 Author Share Posted April 1, 2012 Perfect Thank you so much but I'm still unsure of how I can write [m] if($suboption !== "option1" || $suboption !== "option2" || $suboption !== "option3" || $suboption !== "option4" || $suboption !== "option5" || $suboption !== "option6" || $suboption !== "option7"){ $form->setError($field, "* Unexpected error with option"); }[/m] An easier way Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1333398 Share on other sites More sharing options...
jcbones Posted April 1, 2012 Share Posted April 1, 2012 <?php //php tag for syntax highlighting $options = array('option1','option2','option3','option4','option5','option6','option7'); //array holding options. if(!in_array($suboption,$options)) { //if suboption is not in the options array. $form->setError($field, "* Unexpected error with option"); //set the error. } There are 100's of ways to do things in PHP, doesn't make them wrong, just different. Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1333401 Share on other sites More sharing options...
TimeBomb Posted April 1, 2012 Share Posted April 1, 2012 Hmm. == and != are opposites. Equal to and not equal to. === means equal to and of the same type. Most notably, !== means equal to or not the same type. Some examples: null == false // true null != false // false null === false // false null !== false // true === and !== should not be thought of as direct opposites as == and != are. !== is actually more flexible than !=, and thus it will return true more often than the latter. Personally, out of the four operators, !== is by far my least used one. Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1333404 Share on other sites More sharing options...
zerotolerance Posted April 2, 2012 Author Share Posted April 2, 2012 <?php //php tag for syntax highlighting $options = array('option1','option2','option3','option4','option5','option6','option7'); //array holding options. if(!in_array($suboption,$options)) { //if suboption is not in the options array. $form->setError($field, "* Unexpected error with option"); //set the error. } There are 100's of ways to do things in PHP, doesn't make them wrong, just different. Thank you Hmm. == and != are opposites. Equal to and not equal to. === means equal to and of the same type. Most notably, !== means equal to or not the same type. Some examples: null == false // true null != false // false null === false // false null !== false // true === and !== should not be thought of as direct opposites as == and != are. !== is actually more flexible than !=, and thus it will return true more often than the latter. Personally, out of the four operators, !== is by far my least used one. Thank you! Link to comment https://forums.phpfreaks.com/topic/259919-finding-an-easier-way/#findComment-1333412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.