1

I have a simple question about comparing strings in PHP.

I want to know if two strings (also applicable in any other type) are equal, but I want to exclude null or empty values of the comparison, so, I'm doing this:

if (!empty($var) && $var == $varToCompare) {
    //do stuff
}

It's working fine, but I'd like to know if there exists some operator or function to do this avoiding the empty() part.

My Background: I'd like to know this in a generic way, but here is my background to clarify. I have two or more variables and I need to compare them with variables in my Database, but if I have a NULL in my database and my var is also NULL (or a empty string) I expect false. Even using the === operator if the two strings are NULL or something like "" I receive true. See the example:

$myVars = $this->getArrayOfVars();
$var1 = $myDB->getVar1();// could be null or ""
$var2 = $myDB->getVar2();// could be null or ""
if ((!empty($var1) && $var1 == $myVars[1]) || (!empty($var2) && $var2 == $myVars[2])) {
    //do stuff
    // I just enter here when the strings are equal, but avoiding the null or empty values
}

I checked the docs, but didn't find anything. Someone knows a function that can do this?

4
  • 1
    Isn't empty() generic enough? Commented Jul 13, 2015 at 13:45
  • Keep in mind that both empty(0) and empty('0') are true. Commented Jul 13, 2015 at 13:50
  • 1
    What if in your getArrayOfVars() method if you have a null or "" replace them by " " (space) whith that you will not need the first part of the test (!empty($var)) ? Commented Jul 13, 2015 at 14:01
  • @JayBlanchard. Yes, empty() is good to me, but I belive that the syntax using this is a little hard to read and a function as strcmpIgnoringEmpty() or something would be more readable Commented Jul 13, 2015 at 14:17

4 Answers 4

2

Try is_null() and strcmp() ie,

if(strcmp($var,"")!=0 || is_null($var)){//strcmp returns 0 when strings are equal
}
Sign up to request clarification or add additional context in comments.

Comments

1

The best practice would be to make your own function :

/** return false if any parameter is empty() or $var1 !== $var2 **/
function strictlyEqualAndNotNull($var1, $var2) {
    return (!empty($var1) && $var1 === $var2);
}

which gives :

$myVars = $this->getArrayOfVars();
$var1 = $myDB->getVar1();// could be null or ""
$var2 = $myDB->getVar2();// could be null or ""
if (strictlyEqualAndNotNull($var1, $myVars[1]) || 
    strictlyEqualAndNotNull($var2, $myVars[2])) {
    // do stuff
    // You will enter here when the strings are equal, but avoiding the null or empty values
}

2 Comments

I was looking for a built-in function for this, but apparently create my own is indeed the best option.
Yeah, it is too specific to be a built-in function (in PHP... it may exist on Java's StringUtils...)
0

Why not using strict comparison? Like $one === $two? In case one of them will be string and the other not, it will not work. Link: http://php.net/manual/en/language.operators.comparison.php

4 Comments

But it won't work as asked, i.e if $one is NULL and $two is NULL, the test must return false, whereas $one === $two will return true...
The question was about comparing strings, right? So strings are not null by default.
but it is explicitly said "I want to exclude null or empty values of the comparison"
@t1gor. I know that strings are not NULL by default, but I'm trying to avoid NULL and empty strings (""," ","0", etc...). Thx anyway!
-1

just put @ sign in front of the string instated of empty()

if (@$var && $var == $varToCompare) {
        //do stuff
}



$myVars = $this->getArrayOfVars();
$var1 = $myDB->getVar1();// could be null or ""
$var2 = $myDB->getVar2();// could be null or ""
if ((@$var1 && $var1 == $myVars[1]) || (@$var2 && $var2 == $myVars[2])) {
    //do stuff
    // I just enter here when the strings are equal, but avoiding the null or empty values
}

1 Comment

Please avoid using @. It suppresses errors and makes you code less maintainable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.