Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51977 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71643 invoked from network); 22 Apr 2011 14:27:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Apr 2011 14:27:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=mjpelmear@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mjpelmear@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: mjpelmear@gmail.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:65164] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 83/3A-58227-75091BD4 for ; Fri, 22 Apr 2011 10:27:36 -0400 Received: by iyb12 with SMTP id 12so640006iyb.29 for ; Fri, 22 Apr 2011 07:27:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=QnpR3GFC0o/YFvdsgTXuaOty70z2zNdX6fiQToPjWHI=; b=G3Vn57w/NaGMIvLkck/L1VITjL88WxSmrXcZjF2hYCNw3Bsh9TvviEG7mHiidu9s+s 3RdLJhAn1e+peu5pLncRMtSOBLGxodTd/6xA5V3l7eqEfndjPksRp8BuTeTVYyi6eG41 VdC4v8cLWOTkH7jsuZa/qR1VN1H28SBp77+uw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=h89XYiQsYv+8Zoit71m9DlUjB5zYZZq/PfGhAB6DQV9BY+sg87VG9c/FP+NSC3vKJB wnzH9pepo3GXMlMO5zUFFUfiFNB+JEfokV/G+QFJ/1n3fnpcIVtPwGL37132DbmQc/z4 xyv7ZdAPiJDq23ygSYhbXPfyfAVmmzZnW9J1Y= MIME-Version: 1.0 Received: by 10.42.68.207 with SMTP id y15mr1438034ici.272.1303482453076; Fri, 22 Apr 2011 07:27:33 -0700 (PDT) Received: by 10.42.167.74 with HTTP; Fri, 22 Apr 2011 07:27:33 -0700 (PDT) Date: Fri, 22 Apr 2011 10:27:33 -0400 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: == Comparison type juggling From: mjpelmear@gmail.com (Matt Pelmear) I'm sure this has probably been covered here in the past, but I've been unable to find a good way to search for it, so I'll just ask. if( '20110204024217300000' === '20110204024217300264' ) echo 'BAD'; else echo 'GOOD'; results in "GOOD" being echoed. However, if( '20110204024217300000' == '20110204024217300264' ) echo 'BAD'; else echo 'GOOD'; results in "BAD" being echoed. We guessed that probably the strings were both being converted to ints for the comparison (even on a 64-bit platform 20-digit ints are too large), and indeed the documentation says that "If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically." My questions are: 1) What is the advantage to converting both strings to ints to compare them? We have code that compares values for updating in a database, and in this case we were explicitly treating the data as strings, but don't always do so. It was very non-obvious to us that comparing a string-type to a string-type results in the type translation when using ==. It seems counter-productive to have to do: if( (string) $a === (string) $b ) ..... in every circumstance where $a and $b are be strings representing very large integers. To be clear, I'm referring specifically to circumstances where the type of the objects on both sides of the == comparison are the same. (Maybe int-strings should be a special case, or perhaps this should apply to any object type?) 2) The documentation (http://www.php.net/manual/en/language.operators.comparison.php) only mentions type juggling for ==, !=, and <>, but not greater-than or less-than comparisons. Does this happen with those as well? (If so, I think it could be helpful to have the documentation show this.) 3) If the expected/desired behavior is really to have both strings converted to ints to do the comparison, it would make sense to me to have a "loss of precision" warning- or something to that effect- when the int exceeds the limits of PHP_INT_SIZE-byte ints. However, other than backward compatibility, I have been unable to think of a reason why two strings containing string-representations of ints should be converted for the comparison-- especially with ==. Thanks for your time everybody, Matt