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?
empty()generic enough?empty(0)andempty('0')aretrue.getArrayOfVars()method if you have anullor""replace them by" "(space) whith that you will not need the first part of the test (!empty($var)) ?empty()is good to me, but I belive that the syntax using this is a little hard to read and a function asstrcmpIgnoringEmpty()or something would be more readable