PHP/Data Type/intval — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:03, 26 мая 2010

Checking for a number range

 
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.";
}



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

 
<?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"; 
?>



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

 
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";
?>