note 91345 added to language.types.string

From: Date: Sun, 07 Jun 2009 06:37:12 +0000
Subject: note 91345 added to language.types.string
Groups: php.notes 
Request: Send a blank email to php-notes+get-156427@lists.php.net to get a copy of this message
@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 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 75.172.107.188 ---- Manual Page -- http://www.php.net/manual/en/language.types.string.php Edit -- https://master.php.net/note/edit/91345 Del: integrated -- https://master.php.net/note/delete/91345/integrated Del: useless -- https://master.php.net/note/delete/91345/useless Del: bad code -- https://master.php.net/note/delete/91345/bad+code Del: spam -- https://master.php.net/note/delete/91345/spam Del: non-english -- https://master.php.net/note/delete/91345/non-english Del: in docs -- https://master.php.net/note/delete/91345/in+docs Del: other reasons-- https://master.php.net/note/delete/91345 Reject -- https://master.php.net/note/reject/91345 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156427) next »