When it comes to != and !== which one is the best method to use?

In example

	public static function validateEmail($email)
	{	
		return (filter_var($email, FILTER_VALIDATE_EMAIL) != self::$validEmail);
	}	
	

Or

	public static function validateEmail($email)
	{	
		return (filter_var($email, FILTER_VALIDATE_EMAIL) !== self::$validEmail);
	}	

    I'm assuming self::$validEmail is a string. filter_var returns a string. So !== is not going to test false because its operands are different types, but only if the strings have different values. So it will behave the same as !=.

    !== avoids extra type juggling work in making a comparison. (If self::$validEmail were some kind of EmailAddress object, != would try to call its __toString method to compare it with the string returned by filter_var; !== wouldn't bother and just they they're unequal. It would also say that 12!=='12' because a string is not an integer.) It's also a lot more efficient when comparing two objects (it only returns true if the two objects are the same object). But those cases don't apply here.

      Oh ok, I'd been reading over compare objects I guess I misunderstood it

      As an aside I was recently told I should be using static methods for this class

      <?php  
      
      class Validation 
      {
      	private static 
      	$password,
          $repassword,
          $validEmail,
      	$username;
      
      const MIN_PASSWORD_LENGTH = 8;
      const MIN_USERNAME_LENGTH = 3;
      const MAX_USERNAME_LENGTH = 14;
      
      public static function validateEmail($email)
      {	
      	return (filter_var($email, FILTER_VALIDATE_EMAIL) != self::$validEmail);
      }	
      public static function validatePassword($password)
      {
      	return(strlen($password) >= self::MIN_PASSWORD_LENGTH);
      }
      
      public static function validatecheckbox($agree)
      {
      	return(!empty($terms) >= self::$checked);
      }
      public static function validateRepeatPassword($password,$repassword) 
      { 
      		return $repassword === $password; 
      }  
      public static function validateUsername($username)
      {
      	return strlen($username) >= self::MIN_USERNAME_LENGTH
          && strlen($username) <= self::MAX_USERNAME_LENGTH
          && filter_var($username, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => "/^[a-zA-Z0-9]+$/"]]) !== false;
      }
      }
      
      

      cluelessPHP I was recently told I should be using static methods for this class

      I actually might be inclined not to do so. I think I'd rather pass the values you have as constants into the class via its constructor, so that they can be application settings (and changed as/when desired via the application config settings instead of hard-coded into the class). Thus you would need to instantiate it as an object, eliminating the ability to use static methods that reference those constants.

      cluelessPHP Oh ok, I'd been reading over compare objects I guess I misunderstood it

      I don't know in this case if you've misunderstood it, but in this case since you're not comparing two objects, the identity comparison operator will be checking that its operands are the same type before trying to compare their values. It's just that in this case both operands are already known to be strings.

      That said, using === instead of == can't hurt, unless you deliberately want implicit type casting. In which case it may be better to be explicit about that and do whatever casting is necessary yourself.

      For example, testing if something is false or null should usually be via === so that you don't get a false positive 😏 from a zero or an empty string.

      Testing two objects for equality should also be via ===; it's just a quick lookup of the object hashtable to see if the two operands have the same ID. Using == is a lot slower, because (a) it checks that the properties of the two objects are equal, and (b) recurses on those properties; if there is a cycle of references in there somewhere ($a->foo->bar->baz === $a) you risk blowing the stack.

      If you think you need == to compare two objects then what you probably really want is a custom function that checks the parts that you want to check. Maybe one of the two can provide an "Equals" method that takes the other. Or a static method of the class that takes two arguments. Or maybe in the future PHP will recognise some sort of magic __equals function to be invoked when == is used.

      Write a Reply...