PHP Function Returns Match or No Match
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';
}
}
精彩评论