The post is a part of OOP series tutorials, so it might be useful for you to visit PHP5 – OOP articles. I am going to share knowledge about php primitive data types which are very important for the correction execution of algorithms like upload class, download class, redirect script, csv parser class, random password generator or more. Let’s have a look at the following simple code snippet before going in detail further.

/**
* PHP Primitive Types
*/

/**
* Sum Function
*/

function get_sum( $arg1, $arg2 ) {
	return $arg1 + $arg2;
}

$sum = get_sum( "one", "two" );
echo $sum;

OOPS…

Argument 1 and 2 are passed as string while our “get_sum” function is not expecting this so it returns “0”. As we can’t trust on users for perfect inputs so we must validate all inputs before going to further executions.

Validation is a crucial part of algorithms. I always say “we require a pure oxygen for the prefect beat of our algorithms”.

Let make our function more better to validate our arguments by using the power of PHP type checking functions.

/**
* PHP Primitive Types
*/

/**
* Sum Function
*/

function get_sum( $arg1, $arg2 ) {

	/**
	* Validate Arguments
	* by using PHP Type Checking Function
	* is_integer
	*/

	if( ! ( is_integer( $arg1 ) || is_double( $arg1 ) ) ) {
		return "Argument 1 is not a number";
	}

	if( ! ( is_integer( $arg2 ) || is_double( $arg2 ) ) ) {
		return "Argument 2 is not a number";
	}

	return $arg1 + $arg2;
}

/**
* Test Our Function
*/

echo get_sum( "5", "2.3" ) . "<br />";
echo get_sum( "5", 2.3 ) . "<br />";
echo get_sum( 5, "2.3" ) . "<br />";
echo get_sum( 5, 2.3 ) . "<br />";
echo get_sum( 5, 9 ) . "<br />";
echo get_sum( 5.5, 9.9 ) . "<br />";

Output:

Argument 1 is not a number
Argument 1 is not a number
Argument 2 is not a number
7.3
14
15.4

Great…

We have sanitized our arguments. The execution of script is according to our expectations. I am sure, you have understood the requirement and importance of data validation. I have implemented this crucial part of logic by using the PHP built-in type checking functions. Let explore PHP primitive types and checking functions.

Primitive Types

Type determines the way that data can be managed in your scripts. You use the string type to display character data, for example, and manipulate such data with string functions. Integers are used in mathematical expressions; Booleans are used in test expressions, and so on. These categories are known as primitive types.

Primitive Types and Checking Functions in PHP

Type Checking Function Type Description
is_bool() Boolean One of the two special values true or false
is_integer() Integer A whole number
is_double() Double A floating point number (a number with a decimal point)
is_string() String Character data
is_object() Object An object
is_array() Array An array
is_resource() Resource A handle for identifying and working with external resources such as databases or files
is_null() Null An unassigned value

Checking the type of a variable can be particularly important when you work with method and
function arguments. I hope you have learned a bit, and don’t forget to share your views in the form of comments.