Jason Garber wrote:

Sunday, July 11, 2004, 10:48:06 PM, you wrote:
RL> On Sun, 11 Jul 2004, Jason Garber wrote:

The concept is desirable, but can be achieved if you need it just as
simply using already available syntax (ie a cast):

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


RL> The problem with that is this:

RL>   $level = (int) value($_SESSION['level');

RL> Assume there is no level entry in the session array.  This then
RL> effectively becomes:

RL>   $level = (int) NULL;

RL> Guess what that gives you?  Obviously not what we want here which is why
RL> we are talking about a mechanism to not cast that default value whether it
RL> be the unspecified NULL default, or whatever default is passed in.

What would be the point of casting something unless it was null?  You
still have to do a type-check on the resultant value before you used
it for anything useful.  It seems that in this case, reverting to the
ternary operator or the good old if() statement may be appropriate.

------ Before ------

$level = value($_SESSION['level'], NULL, INT);

if(is_null($level)
{
   //Initialize $level
}

------ After ------

if(isset($_SESSION['level'])
{
   $level = (integer) $_SESSION['level'];
}
else
{
   //Initialize $level
}


I don't mean to be argumentative, but I'm just looking for a application of what you said, as one is not coming to mind.


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


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 == null or $level > 2 */
}


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



Reply via email to