PHP: is_integer() function
The is_integer() function is used to test whether the type of the specified variable is an integer or not. This function is an alias of is_int().
*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value:
TRUE if var_name is an integer, FALSE otherwise.
Value Type: Boolean
" ; > else < echo "$var_name1 is not an Integer
" ; > if (is_integer($var_name2)) < echo "$var_name2 is Integer
" ; > else < echo "$var_name2 is not Integer
" ; > $result=is_integer($var_name3); echo "[ $var_name3 is Integer? ]" .var_dump($result)."
"; $result=is_integer($var_name4); echo "[ $var_name4 is Integer? ]" .var_dump($result)."
"; $result=is_integer($var_name5); echo "[ $var_name5 is Integer? ]" .var_dump($result)."
"; $result=is_integer($var_name6); echo "[ $var_name6 is Integer? ]" .var_dump($result)."
"; $result=is_integer($var_name7); echo "[ $var_name7 is Integer? ]" .var_dump($result); ?>
678 is Integer a678 is not Integer bool(false) [ 678 is Integer? ] bool(true) [ 999 is Integer? ] bool(false) [ 698.99 is Integer? ] bool(false) [ Array is Integer? ] bool(false) [ 125689.66 is Integer? ]
Practice here online :
Previous: is_int
Next: is_long
Follow us on Facebook and Twitter for latest update.
PHP: Tips of the Day
How do I expire a PHP session after 30 minutes?
You should implement a session timeout of your own. Both options mentioned by others (session.gc_maxlifetime and session.cookie_lifetime) are not reliable. I’ll explain the reasons for that.
session.gc_maxlifetime
session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during session start.
But the garbage collector is only started with a probability of session.gc_probability divided by session.gc_divisor. And using the default values for those options (1 and 100 respectively), the chance is only at 1%.
Well, you could simply adjust these values so that the garbage collector is started more often. But when the garbage collector is started, it will check the validity for every registered session. And that is cost-intensive.
Furthermore, when using PHP’s default session.save_handler files, the session data is stored in files in a path specified in session.save_path. With that session handler, the age of the session data is calculated on the file’s last modification date and not the last access date:
Note: If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other filesystem where atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified date) instead of atime. So, you won’t have problems with filesystems where atime tracking is not available.
So it additionally might occur that a session data file is deleted while the session itself is still considered as valid because the session data was not updated recently.
session.cookie_lifetime
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. [�]
Yes, that’s right. This only affects the cookie lifetime and the session itself may still be valid. But it’s the server’s task to invalidate a session, not the client. So this doesn’t help anything. In fact, having session.cookie_lifetime set to 0 would make the session�s cookie a real session cookie that is only valid until the browser is closed.
Conclusion / best solution:
The best solution is to implement a session timeout of your own. Use a simple time stamp that denotes the time of the last activity (i.e. request) and update it with every request:
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) < // last request was more than 30 minutes ago session_unset(); // unset $_SESSION variable for the run-time session_destroy(); // destroy session data in storage >$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
Updating the session data with every request also changes the session file’s modification date so that the session is not removed by the garbage collector prematurely.
You can also use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions like session fixation:
if (!isset($_SESSION['CREATED'])) < $_SESSION['CREATED'] = time(); >else if (time() - $_SESSION['CREATED'] > 1800) < // session started more than 30 minutes ago session_regenerate_id(true); // change session ID for the current session and invalidate old session ID $_SESSION['CREATED'] = time(); // update creation time >
- session.gc_maxlifetime should be at least equal to the lifetime of this custom expiration handler (1800 in this example);
- if you want to expire the session after 30 minutes of activity instead of after 30 minutes since start, you’ll also need to use setcookie with an expire of time()+60*30 to keep the session cookie active.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
is_int
Замечание:
Чтобы проверить, что переменная является числом или строкой, содержащей число (как поле ввода в форме, которое всегда является строкой), используйте is_numeric() .
Список параметров
Возвращаемые значения
Возвращает TRUE , если var является целым числом, или FALSE в противном случае.
Примеры
Пример #1 Пример использования is_int()
$values = array( 23 , «23» , 23.5 , «23.5» , null , true , false );
foreach ( $values as $value ) echo «is_int(» ;
var_export ( $value );
echo «) = » ;
var_dump ( is_int ( $value ));
>
?>?php
Результат выполнения данного примера:
is_int(23) = bool(true) is_int('23') = bool(false) is_int(23.5) = bool(false) is_int('23.5') = bool(false) is_int(NULL) = bool(false) is_int(true) = bool(false) is_int(false) = bool(false)
Смотрите также
- is_bool() — Проверяет, является ли переменная булевой
- is_float() — Проверяет, является ли переменная числом с плавающей точкой
- is_numeric() — Проверяет, является ли переменная числом или строкой, содержащей число
- is_string() — Проверяет, является ли переменная строкой
- is_array() — Определяет, является ли переменная массивом
- is_object() — Проверяет, является ли переменная объектом
is_int
Замечание:
Чтобы проверить, что переменная является числом или строкой, содержащей число (как поле ввода в форме, которое всегда является строкой), используйте is_numeric() .
Список параметров
Возвращаемые значения
Возвращает true , если value является целым числом ( int ), или false в противном случае.
Примеры
Пример #1 Пример использования is_int()
$values = array( 23 , «23» , 23.5 , «23.5» , null , true , false );
foreach ( $values as $value ) echo «is_int(» ;
var_export ( $value );
echo «) color: #007700″>;
var_dump ( is_int ( $value ));
>
?>?php
Результат выполнения данного примера:
is_int(23) = bool(true) is_int('23') = bool(false) is_int(23.5) = bool(false) is_int('23.5') = bool(false) is_int(NULL) = bool(false) is_int(true) = bool(false) is_int(false) = bool(false)
Смотрите также
- is_bool() — Проверяет, является ли переменная булевой
- is_float() — Проверяет, является ли переменная числом с плавающей точкой
- is_numeric() — Проверяет, является ли переменная числом или строкой, содержащей число
- is_string() — Проверяет, является ли переменная строкой
- is_array() — Определяет, является ли переменная массивом
- is_object() — Проверяет, является ли переменная объектом
User Contributed Notes 30 notes
I’ve found that both that is_int and ctype_digit don’t behave quite as I’d expect, so I made a simple function called isInteger which does. I hope somebody finds it useful.
function isInteger ( $input ) return( ctype_digit ( strval ( $input )));
>
var_dump ( is_int ( 23 )); //bool(true)
var_dump ( is_int ( «23» )); //bool(false)
var_dump ( is_int ( 23.5 )); //bool(false)
var_dump ( is_int ( NULL )); //bool(false)
var_dump ( is_int ( «» )); //bool(false)
var_dump ( ctype_digit ( 23 )); //bool(true)
var_dump ( ctype_digit ( «23» )); //bool(false)
var_dump ( ctype_digit ( 23.5 )); //bool(false)
var_dump ( ctype_digit ( NULL )); //bool(false)
var_dump ( ctype_digit ( «» )); //bool(true)
var_dump ( isInteger ( 23 )); //bool(true)
var_dump ( isInteger ( «23» )); //bool(true)
var_dump ( isInteger ( 23.5 )); //bool(false)
var_dump ( isInteger ( NULL )); //bool(false)
var_dump ( isInteger ( «» )); //bool(false)
?>
Keep in mind that is_int() operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.
is_int ( 2147483647 ); // true
is_int ( 2147483648 ); // false
is_int ( 9223372036854775807 ); // false
is_int ( 9223372036854775808 ); // false
?>
In a 64-bit environment:
is_int ( 2147483647 ); // true
is_int ( 2147483648 ); // true
is_int ( 9223372036854775807 ); // true
is_int ( 9223372036854775808 ); // false
?>
If you find yourself deployed in a 32-bit environment where you are required to deal with numeric confirmation of integers (and integers only) potentially breaching the 32-bit span, you can combine is_int() with is_float() to guarantee a cover of the full, signed 64-bit span:
$small = 2147483647 ; // will always be true for is_int(), but never for is_float()
$big = 9223372036854775807 ; // will only be true for is_int() in a 64-bit environment
if( is_int ( $small ) || is_float ( $small ) ); // passes in a 32-bit environment
if( is_int ( $big ) || is_float ( $big ) ); // passes in a 32-bit environment
?>
Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need). He made some errors on his ctype_digit() output though — possibly a typo, or maybe a bug in his version of PHP at the time.
The correct output for parts of his examples should be:
var_dump ( ctype_digit ( 23 )); //bool(false)
var_dump ( ctype_digit ( «23» )); //bool(true)
var_dump ( ctype_digit ( 23.5 )); //bool(false)
var_dump ( ctype_digit ( NULL )); //bool(false)
var_dump ( ctype_digit ( «» )); //bool(false)
?>
As you can see, the reason why using *just* ctype_digit() may not always work is because it only returns TRUE when given a string as input — given a number value and it returns FALSE (which may be unexpected).
With this function you can check if every of multiple variables are int. This is a little more comfortable than writing ‘is_int’ for every variable you’ve got.
function are_int ( ) $args = func_get_args ();
foreach ( $args as $arg )
if ( ! is_int ( $arg ) )
return false ;
return true ;
>
// Example:
are_int ( 4 , 9 ); // true
are_int ( 22 , 08, ‘foo’ ); // false
?>