Jason Garber wrote:

MR> $level = value($_POST['level'], NULL, INT);


MR> switch($level){ MR> case 0: MR> echo "Welcome to level 0"; MR> break; MR> case 1: MR> echo "Welcome to level 1"; MR>
break; MR> case 2: MR> echo "Welcome to level 2"; MR> break; MR> default: MR> echo "That level is invalid. Aborting"; /*
$level == null or $level
2 */
MR> }

This does not in any way call for a cast exception, it can easily be rewritten as:

$level = (integer) value($_POST['level'], -1);

switch($level){ case 0: echo "Welcome to level 0"; break; case 1: echo "Welcome to level 1"; break; case 2: echo "Welcome to level 2"; break; default: echo "That level is invalid. Aborting"; /* $level ==
-1 or $level > 2 }



Don't get me wrong, an third parameter that specified a cast would not hurt (in the way I see it being mainly used):

$x = (int) value($_GET['x'], 0); $x = value($_GET['x'], 0, INT);

Not much different. I'm just saying that it should not be added if the same thing can be accomplished with the same effort with existing
syntax. If a *frequent* use can be demonstrated for it, then I'd be all for it.




That was just a simple example, the point I was trying to make was that
you may want to have a default that is completely outside the range values for that type. What if negative numbers were valid, or may at some point become valid. Given that NULL is such a special value, it can be very useful for differentiating between valid and invalid values.


I also think that what Rasmus suggeted is a good idea:

"So, perhaps introduce the value() function which doesn't do any type
casting and enhance the intval(), strval() and floatval() functions to act like value() but cast appropriately unless the default arg is returned."


If this is the way we went the getval() would probably fit in better with the other function names.


Marc

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to