PHP: Check if variables are exactly the same

hankknight
hankknight used Ask the Experts™
on
In PHP, how can I test if two variables are exactly the same?

If they are both undefined then return true.
If they are both blank then return true.
If they are both defined as strings and set to 'hello' then return true.

The problem with my code is that it does not return true if both variables are undefined.  Instead it throws a warning.


if($zzz===$qqq) return true;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010
Commented:
Use this to check if they are defined:

if (isset($zzz) && isset($qqq)){
  return True;
}else{
  return False;
}

A doubt: you want to retur true if they are blank AND if they are set to 'Hello'? You are trying to perform a multiple check or several different checks?
Marco GasiFreelancer
Top Expert 2010

Commented:
Maybe this makes sense for you?
if (isset($zzz) && isset($qqq)){
  if (($zzz == '' && $qqq == '') || ($zzz == 'Hello' && $qqq == 'Hello')){
    return True;
  }
}else{
  return False;
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
http://www.laprbass.com/RAY_temp_hankknight.php
Outputs
bool(true) bool(false) bool(false) bool(true)

Man page: http://php.net/manual/en/language.operators.comparison.php
<?php // RAY_temp_hankknight.php
error_reporting(E_ALL ^ E_NOTICE);

// DEMONSTRATE TEST FOR IDENTICALITY
function identical($thing1, $thing2)
{
    if ( (!isset($thing1)) && (!isset($thing2)) ) return TRUE;
    if (!isset($thing1)) return FALSE;
    if (!isset($thing2)) return FALSE;
    return ($thing1 === $thing2);
}

// TEST CASES
var_dump(identical($a, $b));

$a = '';
$b = NULL;
var_dump(identical($a, $b));

$a = 3.14;
$b = '3.14';
var_dump(identical($a, $b));

$a = PHP_EOL;
$b = PHP_EOL;
var_dump(identical($a, $b));

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Sandeep KothariProject Lead
Commented:
just these two conditions are good enough:

if(empty($zzz) && empty($qqq){ 
   return true;
}else if($zzz === $qqq){
  return true;
}else{
  return false;
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
@kshna: Please.  just these two conditions - contains a parse error.  That is why EE members are wise to test the solutions posted here instead of assuming that the results will be correct without testing.  A quick count of the number of left and right parentheses in line 1 of the code snippet at ID:36320448 should be enough to show that you do not want to stake your reputation on a comment like that one.  I can tell you're a smart individual, but none of us are so smart that we can post comments and solutions without testing them first.
Most Valuable Expert 2011
Top Expert 2016

Commented:
If you correct the parse error in ID:36320448 by adding the missing parentheses, then you can run a test.  And you will see that the function does not work.  The first test returns TRUE.  The second test returns TRUE and also throws a Notice (not a Warning -- a Notice is different).  The error arises because of a misunderstanding of  PHP type juggling.
http://www.php.net/manual/en/language.types.type-juggling.php

The test case is online on my server here:
http://www.laprbass.com/RAY_temp_kshna.php

On to the next question.  Over and out, ~Ray
<?php // RAY_temp_kshna.php
error_reporting(E_ALL);

// A FUNCTION FROM EE THAT DOES NOT MAKE ACCURATE COMPARISONS
function kshna($zzz, $qqq)
{
   if(empty($zzz) && empty($qqq)){
      return true;
   }else if($zzz === $qqq){
     return true;
   }else{
     return false;
   }
}

// TEST DATA CONTAINING A ZERO AND THE VALUE OF FALSE
$zzz = 1-1;
$qqq = FALSE;
var_dump(kshna($zzz, $qqq)); // RETURNS TRUE - WRONG

// TEST DATA CONTAINING A UNSET VARIABLE AND THE VALUE OF FALSE
unset($zzz);
$qqq = FALSE;
var_dump(kshna($zzz, $qqq)); // RETURNS TRUE - WRONG

Open in new window

Mohamed AbowardaSenior Software Engineer
Commented:
If they are both undefined then return true.
If they are both blank then return true.
If they are both defined as strings and set to 'hello' then return true.

The idea is that you convert that to programmatical code to create a function to do what you want.

Example would be:
Converting "If they are both undefined then return true" to programmatical code:
if(empty($zzz) && empty($qqq)) return true;

Open in new window


Or

if(!isset($zzz) && !isset($qqq)) return true;

Open in new window

Commented:
<?php
/* for experts-exchange.com
   by  Pascal LEGAL
*/

// By default $zzz and $qqq are not defined
// just uncomment next lines to define them
// $zzz = 3;
// $qqq = '3';
	
if (!isset($zzz) && !isset($qqq)) {
	$r = true;
} else if (!isset($zzz) || !isset($qqq)) {
	$r = false;
} else if ($zzz === $qqq) {
	$r = true;
} else {
	$r = false;
}

echo $r;
?>

Open in new window

Commented:
Some codes dont works if there is only one defined variable so you can use this :

<?php
/* for experts-exchange.com
   by  Pascal LEGAL
*/

// By default $zzz and $qqq are not defined
// just uncomment next lines to define them
// $zzz = 3;
// $qqq = '3';
	
if (!isset($zzz) && !isset($qqq)) {
	// if both are not defined return true
	$r = true;
} else if (!isset($zzz) || !isset($qqq)) {
    // if only one is defined return false
	$r = false;
} else if ($zzz === $qqq) {
	// if both are defined and equals return true
	$r = true;
} else {
	// if both are defined and not equals return false
	$r = false;
}

echo $r;
?>

Open in new window


ps : sorry for the uncompleted precedent comment.
Sandeep KothariProject Lead

Commented:
@Ray_Paseur: wooha ... what a gr8 analysis dude... seriously ...but if you had put this much focus on the solution of this problem than it would have been helpful for the auther ...instead of trying to find mistakes in the incorrect solutions... what are you egoistic or jealous ?...anyways i dont care !...
Most Valuable Expert 2011
Top Expert 2016

Commented:
Hi, @kshna.  I am egotistical, meticulous, analytical and demand the best from myself every day starting at sunrise.  But I am also human, lazy, flawed and stupid.  And so I work very hard to try to devise test-driven solutions that can help me get the best results for my clients.  They pay me a lot of money for that.  It's no secret.

On to the next...

Best regards, ~Ray
Sandeep KothariProject Lead

Commented:
@Ray.. then put for effort to find solutions only not the faults in others solution ..... and if you are egoistical than be silent because if you throw your ego on someone else it may bounce back really hard as the other person might have a very strong ego than you mite have ever imagined... so save your energy to make your clients happy and stay happy....dont interrupt !.... keep moving next but dont shout about it !
Commented:
I came up with this, and tested it.  It fulfills all the requests

 
<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);


if ((!isset($zzz) && !isset($qqq)) || ((strlen($zzz) == 0) && (strlen($qqq) == 0)) || (empty($zzz) && empty($qqq)) || (!is_numeric($zzz) && !is_numeric($qqq)) || (($zzz == 'hello') && ($qqq == 'hello'))){
	echo 'true';
}else{
	echo 'false';
}

?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial