1

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?

In PHP and Javascript code i'm seeing more === out there as opposed to what I'm used to == for equality. Is === just a fancy way to write == ? Whats the deal with the xtra = sign?

PHP Code example:

<?php

// Parsing Yahoo! REST Web Service results using
// unserialize. PHP4/PHP5
// Author: Jason Levitt
// February 1, 2006

error_reporting(E_ALL);

// output=php means that the request will return serialized PHP
$request =  'http://yahoopipesURL';

$response = file_get_contents($request);

if ($response === false) {
 die('Request failed');
}

$phpobj = unserialize($response);

echo '<pre>';
print_r($phpobj);
echo '</pre>';

?>

Javascript Code example from one of my questions:

function setOrCreateMetaTag(metaName, name, value) {
    var t = 'meta['+metaName+'='+name+']';
    var mt = $(t);
    if (mt.length === 0) {
        t = '<meta '+metaName+'="'+name+'" />';
        mt = $(t).appendTo('head');
    }
    mt.attr('content', value);
}

setOrCreateMetaTag(name, viewport, 'width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0');
Community
  • 1
  • 1
Chamilyan
  • 9,347
  • 10
  • 38
  • 67
  • Yep, thanks. It is a dupe. It's kind of funny that i couldn't find it in search because == and === is a site operator of some kind and doesn't work in the search box. – Chamilyan Sep 20 '10 at 07:41
  • Don't feel bad, you *cannot* search for such characters. I found it on the FAQs, which are also very obscure - http://stackoverflow.com/tags/javascript/faq . Also, I've posted a link to JavaScript, but the idea is the same in PHP. – Kobi Sep 20 '10 at 07:43
  • Thanks :). I've lived in the QA world for 10+ years so I'm always checking for dupes on auto pilot. This one slipped by me, but now I'll only feel a little bad. – Chamilyan Sep 20 '10 at 08:02

4 Answers4

3

Since PHP and JavaScript are weakly typed, sometimes using == will cause unwanted coercions (e.g., convert a string to a number). The === operator is an identity operator—it only returns true if both operands are the same object.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
2

== is a value based comparison on javascript. Such as

x = 5
x == '5' //True

=== is a value and type based comparison so;

x = 5
x ==='5' //False, becaouse diffrent types
Mp0int
  • 18,172
  • 15
  • 83
  • 114
1

'===' means exactly equal whereas '==' means equivalent within the current context.

This makes a big difference when comparing numbers as the following code snippet illustrates (stolen from the php docs):

<?php
$first = 10;
$second = 10.0;
$third = "10";

if ($first == 10) print "One";
if ($second == 10) print "Two";
if ($third == 10) print "Three";

if ($third === 10) print "Four";
if ($second === 10) print "Five";
if ($first === 10) print "Six";
?>
Will print out
OneTwoThreeSix
James Anderson
  • 27,109
  • 7
  • 50
  • 78
0

in PHP, the extra = sign makes it absolute equality operator (===), which ensures/tests whether two values are the same and of the same data type, thus adding precision to comparisons. Here's a nice read from tuxradar: http://www.tuxradar.com/practicalphp/3/12/2

bhu1st
  • 1,282
  • 11
  • 23