note 55694 deleted from function.abs by hholzgra

From: Date: Sun, 15 Apr 2012 19:26:47 +0000
Subject: note 55694 deleted from function.abs by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187142@lists.php.net to get a copy of this message
Note Submitter: alreece45 at yahoo dot com ---- Both of the below codes were wrong. <?php // Check to see if $range is numberic, if not, set it to the integer value 1 if(!is_numeric($range)) { $range=1; } // Check to see if $range is an integer and not a float. Use is_int() and not int(). if(!is_int($range)) { // Make it an integer. Use intval() here, not int_val. $range=intval($range); } $range=abs($range); ?> The only thing I don't get is why we have to check before doing the functions. Whether or not we do the functions it will give us what we want. The only check I really understand being there is the one that sets it to one. Why not just do: <?php // If $range is numberic, make it in positive integer, otherwise, make it one. $range = is_numeric($range) ? abs(intval($range)) : 1; ?> Seems like a lot less code. Or If you prefer to stay with if statements: <?php if(is_numberic($range)) { // If $range is numberic, make it in positive integer. $range = abs(intval($range)); } else { //otherwise, make it one. $range = 1; } ?> Both ways seem smaller to me.

« previous php.notes (#187142) next »