问题
In my case should I use != as below, or is !== more appropriate, what is the difference.
private function authenticateApi($ip,$sentKey) {
$mediaServerIp = '62.80.198.226';
$mediaServerKey = '45d6ft7y8u8rf';
if ($ip != $mediaServerIp ) {
return false
}
elseif ($sentKey != $mediaServerKey ) {
return false
}
else {
return true;
}
}
public function setVideoDeletedAction(Request $request)
{
//Authenticate sender
if ( $this->authenticateApi($request->server->get("REMOTE_ADDR"),$request->headers->get('keyFile')) != true ) {
new response("Din IP [$ip] eller nyckel [********] är inte godkänd för denna åtgärd.");
}
回答1:
!= checks value
if($a != 'true')
!== checks value and type both
if($a !== 'true')
回答2:
http://www.php.net/manual/en/language.operators.comparison.php
As the manual says, one compares type as well.
回答3:
!==
is more strict. '1'==1
returns true, but '1'===1
- false, because of different types.
回答4:
===
and !==
are used to compare objects that might be of different type. ===
will return TRUE iff the types AND values are equal; !==
will return TRUE if the types OR values differ. It is sometimes known as the 'congruency' operator.
If you are comparing two things that you know will always be the same type, use !=.
Here's an example of using !==. Suppose you have a function that will return either an integer or the value FALSE
in the event of a failure. 0 == FALSE
, but 0 !== FALSE
because they are different types. So you can test for FALSE
and know an error happened, but test for 0
and know you got a zero but no error.
来源:https://stackoverflow.com/questions/20709321/difference-between-and