note 66390 added to function.is-scalar

From: Date: Sun, 21 May 2006 02:02:56 +0000
Subject: note 66390 added to function.is-scalar
Groups: php.notes 
Request: Send a blank email to php-notes+get-111748@lists.php.net to get a copy of this message
just a warning as it appears that an empty value is not a scalar. although the manual says scalars are: -> integers (eg. 243) -> floats (eg. 14.4) -> strings (eg "this is a string") -> booleans (eg true or false) this might seem easy but rembere that empty values are: -> NULL -> false -> empty string -> 0 -> "0" thus the true list of scalars must be: -> integers large then zero -> floats larger then zero -> strings with a lenght large then zero -> string that do not contain only the number 0 -> boolean value of true. especialy the boolean value false can be tricky as true is considerd to be scalar but false is not. or a string that is initialized with a zero lenght here's an example. $string = ""; //initialize our string. function store_var($var) { if(is_scalar($var)){ print("our var is a scalar."); }else{ print("you tried to store something else here."); } } $this->store_var($string); // does not work as expected this might be usefull when you create method to store a variable inside a class but do not want to store other stuff like object, arrays or resources, but storing an empy value is oke. then is_scalar does not work as empty values are incorrectly not seens as a bool, string, integer or float. this is an important downside to is_scalar wich might cause some people to loose there hair from headscrachting. this might be added to the manual as a warning or something. ---- Server IP: 83.137.20.120 Probable Submitter: 62.216.10.14 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.is-scalar.php Edit -- http://master.php.net/note/edit/66390 Del: integrated -- http://master.php.net/note/delete/66390/integrated Del: useless -- http://master.php.net/note/delete/66390/useless Del: bad code -- http://master.php.net/note/delete/66390/bad+code Del: spam -- http://master.php.net/note/delete/66390/spam Del: non-english -- http://master.php.net/note/delete/66390/non-english Del: in docs -- http://master.php.net/note/delete/66390/in+docs Del: other reasons-- http://master.php.net/note/delete/66390 Reject -- http://master.php.net/note/reject/66390 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111748) next »