PHP/Data Type/intval

Материал из Web эксперт
Версия от 10:03, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Checking for a number range

   <source lang="html4strict">

if ($_POST["age"] != strval(intval($_POST["age"]))) {

   $errors[] = "Your age must be a number.";

} elseif (($_POST["age"] < 18) || ($_POST["age"] > 65)) {

   $errors[] = "Your age must be at least 18 and no more than 65.";

}

 </source>
   
  


intval() function: pass a second parameter that specifies the base for the conversion

   <source lang="html4strict">

<?php echo intval("123", 10) . "\n"; echo intval("101010", 2) . "\n"; echo intval("123", 8) . "\n"; echo intval("123", 16) . "\n"; echo intval("H123", 32) . "\n"; echo intval("H123", 36) . "\n"; ?>

 </source>
   
  


intval() function: pass a second parameter that specifies the base to use for the conversion.

   <source lang="html4strict">

The default value is 10, but it"s possible to use base 2 (binary), 8 (octal), 16 (hexadecimal), or any other value such as 32 or 36 <?php echo intval("123", 10) . "\n"; echo intval("101010", 2) . "\n"; echo intval("123", 8) . "\n"; echo intval("123", 16) . "\n"; echo intval("H123", 32) . "\n"; echo intval("H123", 36) . "\n"; ?>

 </source>