php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #23721 TRUE and FALSE constants slower than keywords in previous versions
Submitted: 2003-05-20 11:43 UTC Modified: 2004-02-17 22:16 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: max at fuck dot org Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.1 OS: FreeBSD
Private report: No CVE-ID: None
 [2003-05-20 11:43 UTC] max at fuck dot org
-bash-2.05b$ cat true_test.php   
<?php /* -*- C++ -*- */

function getmicrotime(){ 
  list($usec, $sec) = explode(" ",microtime()); 
  return ((float)$usec + (float)$sec); 
} 

$time_start = getmicrotime();


for ($i = 0; $i <= 1000000; $i++) {

  if ($i % 2) {
    $n = true;
  }
  else {
    $n = false;
  }

  if ($n == true) {
  }
  else if ($n == false) {
  }
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "true/false:\t $time seconds.\n";
$time_start = getmicrotime();

for ($i = 0; $i <= 1000000; $i++) {

  if ($i % 2) {
    $n = TRUE;
  }
  else {
    $n = FALSE;
  }

  if ($n == TRUE) {
  }
  else if ($n == FALSE) {
  }
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "TRUE/FALSE:\t $time seconds.\n";

?>

-bash-2.05b$ php -v
4.2.3

-bash-2.05b$ uname -a
FreeBSD 4.8-RELEASE FreeBSD 4.8-RELEASE #0: Thu Apr  3 10:53:38 GMT 2003     

-bash-2.05b$ php true_test.php 
X-Powered-By: PHP/4.2.3
Content-type: text/html

true/false:      5.9379440546036 seconds.
TRUE/FALSE:      5.9383209943771 seconds.

<configure/make/make install-cli 4.3.1 (no special config options)>

-bash-2.05b$ php -v
PHP 4.3.1 (cli) (built: May 20 2003 11:15:26)
Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies

-bash-2.05b$ php true_test.php 
true/false:      4.2005170583725 seconds.
TRUE/FALSE:      5.1550530195236 seconds.

<configure/make/make install-cli latest snapshot (no special config options)>

PHP 4.3.2RC4-dev (cli) (built: May 20 2003 11:41:28)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies

-bash-2.05b$ php true_test.php 
true/false:      4.2775070667267 seconds.
TRUE/FALSE:      5.1591459512711 seconds.


I've tested this on multiple machines, seems that anything newer than 4.3.0 has this issue. Although I haven't looked myself I was told that recently TRUE and FALSE are now defined as constants rather than keywords. While these are both faster than the previous version(s), if this difference in speed was intended, it wasn't documented anywhere.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-20 18:00 UTC] sniper@php.net
If better performance is a problem for you, maybe you should consider using some slower language? :)

There is no bug.

 [2003-05-20 19:12 UTC] max at fuck dot org
no, i think it's great the performance is better, but all of a sudden there is a performance difference between using true and TRUE. if true is (as it would seem) the faster and more efficient way of using the keyword, maybe someone should go through the documentation and make a note of that?
 [2003-05-20 20:08 UTC] sniper@php.net
In real life applications you really won't notice any difference. And the lowercase version of any constant defined 
as case-insensitive will always be looked up faster.
(ie. true is looked for first, then if not found, TRUE)

 [2003-05-21 02:01 UTC] philip@php.net
Use of proper case is faster than "improper" case for case insensitive constants.  This includes true/false, null, and define()'s third parameter.  Not sure what others... or even if this really should be documented? :)  AFAICT, this behavior does not affect magical case insensitive constants such as __file__.

This difference is as of 4.2.3
 [2003-05-21 03:11 UTC] derick@php.net
Why do you use 1000000 times TRUE or FALSE in your programs anyway?
 [2003-05-21 08:57 UTC] max at fuck dot org
I don't think I would ever use true/false a million times. This came up when talking to someone about which way was 'proper', whereas before it didn't seem to make a difference and now to allow people who want to write 'True' to get their way, it has been changed. While I don't think this is a big performance problem or anything, I think in conjuction with other little slow-downs on a high load site it could have an effect on the speed. It's obviously faster than older versions regardless of how you use it, but I don't think I have ever seen it used in lowercase anywhere on the php website.
 [2003-05-21 10:05 UTC] sniper@php.net
Code readability always outweights minor performance gains, IMO. :)


 [2003-05-21 11:14 UTC] philip@php.net
PHP groupees sometimes mention:

a) ++$i  is faster than $i++
b) 'foo' is faster than "foo"
c) echo  is faster than print
d) str_* is faster than regex
e) And now true is faster than TRUE!

Woohoo! My PHP scripts will be blazing fast!!! :)
 [2004-02-17 22:16 UTC] irchtml@php.net
The language.types.boolean states that the TRUE/FALSE keywords are case-insensitive.

I don't believe this requires any updates to the docs, unless someone is persistent and wants to re-open.

Status -> Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 20 17:00:03 2025 UTC