× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

PHP Data Types

In this article we will see how to work with data types in PHP.

PHP data types

Figure 1: PHP data types

To the programmers that are willing to learn a new programming language, a fundamental step of importance is the knowledge of the kind of data that the programming language supports. For instance, many high-level languages possess many kinds of data to represent numerical values. The decision of which to use will depend on factors like the necessary precision or the maximum value that is possible to store in a variable.

Note 1: Regarding the attribution of decimal values to variables of the integer type, many languages keep only the integer part and discard the decimal part. Details of that behavior are not the focus of this article.

In PHP this is not necessary. The language is extremely flexible to the point that it is not necessary to declare the type of the variable on the moment of the declaration. In addition , it is possible to attribute values of different types to the same variable.

To quickly demonstrate how it is done, , in listing 1, we have a variable called $test which receives values of three different kinds of data.

Listing 1: Variable declaration and attribution of non-typed values

	$test = "John";
	$test = 1;
	$test = TRUE;

The code above can be executed without any errorrs ., This loose typing is supported in many other programming languages.

Note 2: The syntax of the language is not the focus of this publication, but it is important to remember that the variables are defined with a dollar symbol followed by the name of the variable, as can be seen above.

It have been seen how the definition of variables and attribution of values is done. We can now move to the theme that motivated the writing of this article and that will be, in fact, the focus of this publication.

Kinds of data on PHP

Despite of the fact that it is not strongly typed, the language has some kinds of data which, on practical terms, only differ from one another in the moment of the manipulation,.

These kinds are:

  • Boolean;
  • Numeric;
  • String;
  • Array;
  • Object;
  • Resource;
  • Mixed;
  • Callback;
  • NULL.

Boolean type

The boolean kind of data represents a logic value that can be either TRUE or FALSE. It means, this kind of variable is not used in arithmetical operations. For instance, it's possible values are only these two and are used in order to do the verification and the attribution of conditions.

In PHP the values TRUE and FALSE are represented by the reserved words TRUE and FALSE, respectively. Next, we have an example of the utilization of this kind of data. First of all, it is attributed values to two variables and after that some conditions are defined as these values base.

Listing 2: Basic utilization on boolean type

	$day = TRUE;
	$rain = FALSE;

	if($day == TRUE)
		echo "it's day.";
		echo "it's night.";

	if($rain == TRUE)
		echo "it's raining.";
		echo "it isn't raining.";

First of all, it is attributed the values true and false to two variables $day and $rain, respectively. Then it is verified the value of this variables and, depending on it, are exhibited messages.

Note 3: As we are verifying if the value of the variable is true, the operator of equality and the value TRUE are optional. It would only take to use, for instance, if($day) or if($rain) and automatically the verification would be the same.

Due the previously cited flexibility, it is easy to do the verification of veracity of a variable from other values that are not TRUE and FALSE. For example, the value 1 is considered true, while the value NULL is considered false. Next are listed the possible values and their reviews.

  • Numerical values different from 0 (zero) are considered as TRUE, while the zero is considered FALSE.
  • String values containing something are considered TRUE, while empty strings ("") and the text "0" are recognized as FALSE.
  • Empty arrays or objects without any content are considered as FALSE, the contrary is logically TRUE.
  • The value NULL is considered FALSE.

Listing 3 shows an example of this kind of attribution to boolean variables.

Listing 3: Use of boolean variables with diverse values

	$var = "TEXT";
		echo "The var's value is true";
		echo "The var's value is false";

As it was expected, it will display the message "The value of the variable is true".

Numeric type

Numeric types can be represented in several notations in PHP . On the decimal base, octal, hexadecimal.In the case of decimal, it can also be written as scientific notation.

With this kind of data, it is possible to do the most diverse arithmetical operations, obtaining as the result a numerical value (as it was to be expected).

Below we have some values attributed to variables of the numerical type.

Listing 4: Attribution of values to numerical variables

	$a = 10; //decimal
	$b = -123; //octal
	$c = 0123; //hexadecimal
	$d = 0x1A; //float point
	$e = 5e21; //scientific notation

It is important to know that the decimal separator is the period, and not the comma as we're used to in Brazil, this one is not used on attribution of numerical values.

String type

This kind represents a chain of alphanumeric characters (letters, numbers and special characters). It means, it is used to manipulate texts. In order to attribute values to a String variable, simple or double quotes can be used, as can be seen below.

Listing 5: Example of string's use

	$name = "Joel Rodrigues";
	echo $name;

	$site = 'Mr. Bool';
	echo $site;

Note 4: Despite both forms are equally allowed (simple or double quoted), it is recommended to use, as a good way of working, only one kind of attribution.

PHP gives us many native functions to manipulate strings. Such functions are explained in this article: http://mrbool.com/php-functions-to-work-with-strings/26799.

Array type

Arrays are lists of values that can store in its structure many kinds of data.

in most languages, an array can only contain elements of the same kind of data and its capacity must be defined before it is started to add values to the list. That informs not only about arrays on C# language, but also presents the theory of functioning of these structures that, in general, are well alike on many languages.

Working with arrays involves a series of methods, routines and techniques that, only by themselves, could be a theme of an article (as saw on the cited link). In this article, however, we will only see simple examples of declaration and attribution of values to arrays.

Array can be declared and initialized at the same time, using the key word "array" followed by the values in parenthesis and separated by commas, as we have seen on Listing 5.

The access to each element of the array, on the other hand, is given through the use of brackets containing the index of the element (position on list, starting at zero).

Listing 6: Example of an array declaration and initialization together

	$countries = array("Brazil", "USA", "Italy");
	echo $countries[0];

In the code above we have created an array called $countries containing four elements of the String type. Next we have exhibited the value of the first element of the array (Brazil).

Another way to add elements is attributing values to each position individually. In order to do that, we use the same syntax of access previously discussed. In the code below we have an array $languages to each we will add four elements and, after that, we exhibit the value of the fourth element (Ruby).

Listing 7: Adding of elements to the array individually

	$languages[0] = "PHP";
	$languages[1] = "C Sharp";
	$languages[2] = "Delphi";
	$languages[3] = "Ruby";
	echo $languages[3];

Object type

Objects are fundamental elements of object oriented programming and can be defined as "entities that possess certain kind of behavior defined by its methods and properties". In fact, to early learners, these terms can be kind of confusing. As it is not the focus of this article to present and discuss in detail the object oriented concepts , we will see just a basic example of use of an object.

In order to understand the functioning of the next code, it just takes to have in mind one simple definition of Class. Then, Class can be defined, roughly, as "a kind of data created by the user (programmer) and that possess on its structure attributes (variables) and methods that define its functioning".

On the next Listing, for instance, we will create a class called Book that possesses the properties (attributes) Title and Author. We create an object of the kind Book and define the value of its attributes.

Listing 8: Basic example of the use of an object

	class Book
		var $Title;
		var $Author;

	$exemplar = new Book();	
	$exemplar->Title = "Microsoft Visual C# 2005 - Step by Step";
	$exemplar->Author = "John Sharp";

Resource type

The resource type is a special kind of variable used to store references to PHP external resources, such as an image, open files or connections with data banks.

This kind of data is, in general, created by special functions as the mysql_connect, that starts a connection to a MySql data bank, keeping a reference to it that can be used afterwards.

As have been said, this kind of data is used by special functions and with special finalities, thus, it is not interesting or necessary, at this point, to show practical examples of use. Then we stay only with the conceptual definition.

Mixed type

As the name implies, the mixed type, indicates that one variable can receive values of many types. One of the most common examples of it is the method gettype that receives a parameter of the type mixed and return its type, that can be any one in PHP.

Callback type

The callback type can be understood as a reference to a function. Some functions receive as a parameter one "callback function", it means, a function that must be executed when the processing of the first be completed. One of the most common examples is the function call_user_func that receives as a parameter one reference to another function to be executed.

On the code below we will see a basic example of use.

Listing 9: Example of callback's use

	function ShowMessage()
		echo "Wellcome.";


We have the function ShowMessage which is called from another, the call_user_func. The call_user_func also can be used to call methods of a class or object or specific objects. For more details, it is suggested the reading the official documentation.

NULL type

The value null is widely used in several languages to indicate that a variable does not possess a value. When we attribute 0(zero) to a number or "" (empty string), despite of it seems that this variables does not possess values, it is not true, because even these are stored into the memory. The unique possible value for the NULL type is the NULL itself.


As have been seen, the manipulation of variables in PHP does not differ a lot between the types, which makes this language one of the most practical to work with and easy to learn.

I wrap up this article and I hope that this content may be of use to the programmers, specially to those who are beginning to learn PHP.

See you next time.

Microsoft MVP, Bachelor in Science And Technology, Editor and Consultant at DevMedia with more than 7 years of experience in software development.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login