note 91345 deleted from language.types.string by danbrown

From: Date: Mon, 08 Jun 2009 12:38:52 +0000
Subject: note 91345 deleted from language.types.string by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156454@lists.php.net to get a copy of this message
Note Submitter: uramihsayibok, gmail, com ---- @Jarobman: (really should have added this to the comment I just made. an editor would be welcome to do that.) You've stumbled onto a PHP "feature": undefined constants are automagically converted into strings. If you had your error_reporting up enough with display_errors on you'd have seen the notice issued: "Notice: Use of undefined constant X - assumed 'X'..." Thus: <?php // $object is an object, membervar is a constant // except membervar doesn't exist, so it's converted to a string // the minus forces $object and membervar as numbers: // - $object becomes an int(1) (with a Notice issued) // - membervar becomes an int(0) (since "membervar" doesn't resemble a number) // so $object-membervar === 1 some_function($object-membervar); // some_function(1) // constants can begin with a _ so there's no error // (just three notices about the constant->string conversion) echo hello.world._6789; // "helloworld_6789" // PHP accepts statements that don't actually do anything // examples include: thisisastandalonestring; // "thisisastandalonestring"; false; 1; ; // string -> "string" -> 0 // anotherstring -> "anotherstring" -> 0 // yetanotherstring -> "yetanotherstring" -> 0 echo string+anotherstring-yetanotherstring; // "0" // (string)true is "1" // (string)false is "" echo true.false.concatenated; // 1concatenated // surprisingly this doesn't work - you'd expect "cantdothis1234567890" echo cantdothis.1234567890; // parse error, expecting "," or ";"... // PHP parses the entire line before it tries to execute // .1234567890 is a float and so PHP sees // (echo function) (constant) (float) (semicolon) // which is not valid syntax for anything // since it breaks at the (float) stage, PHP looks for a syntax that works, // and finds that after (constant) you could have a comma or semicolon ?>

« previous php.notes (#156454) next »