Learning PHP for absolute beginners – data types

PHPPHP can work with 8 different types of data, which can be separated into 3 categories: scalar data types, compound data types and special data types.

Not that scary as it might sound. You already know half of that stuff. Of course if you attended math classes at school 🙂 … I did not 😉

Scalar data types

Data Type Example Description
integer 10, -74 A whole number, can be either a positive or negative number
double/float 128.56, -55.015 A floating-point number
string “Hello, world!” A series of characters
boolean true, false Used for values that are either true or false. Most commonly used when working with conditional statements

I hope this part is clear, maybe except for the Boolean data type, but don’t worry we’ll be working with that in our next tutorial. Now let’s see what compound data types have to offer.

Compound data types

Data Type Description
array Stores multiple values that are indexed by numbers or strings
object May contain multiple properties and methods

What’s in common is that both of them can store multiple values and that is all we need to know for now.

Special data types

Data Type Description
resource Used to access external resources, like database connection or file handle
null Means the variable has not been assigned any value

Not that widely used. I’ll try to think of some examples, but that’ll be much… much later 🙂

Loose typing in PHP

PHP is a loosely-typed language, which means you can change variable’s data at any time. For example, in Java once you declare a variable and its data type, you can’t change it – the variable must always contain only the data of the type we defined. Here’s some code example

int year = 2012;
year = 'text'; // this statement is wrong, cause we declared the variable as an integer

year – is the name of the variable we declare, int – is the data type assigned to it – integer. So the variable year can now only contain integer values, otherwise we’ll get an error. On the contrary in PHP we don’t need to think about that stuff:

$year = 2012;
$year = 'The end of the world is scheduled for '.$year;
echo $year;
// The result of echo would be: "The end of the world is scheduled for 2012"

How to check data type of the value

We can easily determine the data type using the gettype() function. We just need to pass a variable name into this function – gettype($var_name). Let’s play with some examples and check the result

$var;
echo gettype($var) . '
'; // displays "NULL" because we did not assign any value to the variable $var = 58; echo gettype($var) . '
'; // displays "integer" $var = 12.52; echo gettype($var) . '
'; // displays "double" $var = "wassup"; echo gettype($var) . '
'; // displays "string"

You can also check if the variable contains a specific data type using the following functions:

is_int( $var ) Returns true if value is an integer, false otherwise
is_float( $var ) Returns true if value is a float, false otherwise
is_string( $var ) Returns true if value is a string, false otherwise
is_bool( $var ) Returns true if value is a boolean, false otherwise
is_array( $var ) Returns true if value is an array, false otherwise
is_object( $var ) Returns true if value is an object, false otherwise
is_resource( $var ) Returns true if value is a resource, false otherwise
is_null( $var ) Returns true if value is NULL, false otherwise

And here are some code examples for you to play with:

$var = 69;
echo is_int($var); // returns 1 which means TRUE

echo is_string($var); // The browser won't return anything to you, as the statement is FALSE

Using PHP conditional statements we could improve the browser output:

$var = 'Hello';
if ( is_string($var) ) {
  echo "Yeah! It's a string";
}else{
  echo 'The value of the variable is not string data type';
}

Using if…else statement we check if the variable is a string data type. If it’s true, then output ‘Yeah! It’s a string’ into the browser, otherwise output ‘The value of the variable is not string data type’.

Conclusions

You’ve learned all about PHP data types. You discovered different data types such as scalar, compound and special. We’ve also learned about PHP’s loose typing, and how to check the data type of a variable. Happy coding!

Questions? I’m waiting for them, guys and gals 🙂

About The Author

Tobias

I'm keen on developing websites. Any sentence that has HTML/CSS/PHP in it, will most likely generate interest for me )