Dev Corner

Software Developer’s Notepad

It is useful to know the type of the variables, with which you are operating. But in PHP there are not declarations, in which we point out what the type of the variable to be. So if we say that we have a input form and we have to operate only with numbers, how do we know that the user inputs a number? He can actually enter everything, including strings, empty space and etc.

There are a bunch of functions, which see what actually the variable contains.

bool is_numeric($var) – this function checks if var is a number or a numeric string. It returns TRUE if var is a numeric string or FALSE otherwise.

Example #1

<?php
	is_numeric(345);		// Returns true
	is_numeric("456"); 		// Returns true	
	is_numeric("adf");             	// Returns false	
	is_numeric("345dffds");     	// Returns false
?>

bool is_int($var) – this function checks if var is a integer. It returns TRUE if var is a integer or FALSE otherwise.

Example #2

<?php
	is_numeric(345);		// Returns true
	is_numeric(345.345);		// Returns false
	is_numeric("456"); 		// Returns false	
?>

bool is_double($var) – this function checks if var is a double. It returns TRUE if var is a integer or FALSE otherwise.

Example #3

<?php
	is_numeric(345.345);		// Returns true
	is_numeric(345);		// Returns false
	is_numeric("456.68"); 		// Returns false	
?>

This three functions are enough to see if the variable is a number. There are also another functions for checking what the type of a variable is, such is_bool() , is_array() , is_string() , is_object() etc.

Add A Comment

You must be logged in to post a comment.