开发者

PHP Function Returns Match or No Match

开发者 https://www.devze.com 2023-02-12 23:42 出处:网络
I am new to PHP and am trying to write a simple function that takes two variables and returns the string \"match\" if the variables are the same and returns \"no_match\" if they are different. Again n

I am new to PHP and am trying to write a simple function that takes two variables and returns the string "match" if the variables are the same and returns "no_match" if they are different. Again new 开发者_如何学JAVAto programming, so thanks in advance!!


You don't need a function to do this:

$result = ($var1 === $var2) ? "match" : "no_match";

But if you insist:

function matches($var1, $var2, $strict = false) {
   return ($strict ? $var1 === $var2 : $var1 == $var2) ? "match" : "no_match"
}

Usage:

$v1 = 1;
$v2 = "1";

var_dump(matches($v1, $v2)); //match
var_dump(matches($v1, $v2, true)); //no_match

$v1 = "1";

var_dump(matches($v1, $v2, true)); //match


/**
 * Compare two values for equality/equivalence
 * @param mixed
 * @param mixed
 * @param bool compare equivalence (types) instead of just equality
 * @return string indicating a match
 */
function compare($one, $two, $strict = false) {
   if ($strict) {
      $compare = $one === $two;
   }
   else {
      $compare = $one == $two;
   }
   if ($compare) {
      return 'match';
   }
   else {
      return 'no_match';
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消