Variables and Data Types
Explore how to declare and use variables in PHP, understand different data types such as null, boolean, integer, float, string, array, and learn the role of constants in efficient coding. This lesson helps you grasp how PHP handles variable types dynamically and prepares you to manage data effectively in your programs.
Variables
A variable in any programming language is a named piece of computer memory, containing some information inside. Variables are one of the essential parts of a computer program. You can declare a variable in PHP using a $ sign followed by its name, e.g., $myVariable.
Consider the following PHP code where we store data in two variables and print them.
The $str variable contains a string, and the $num variable contains an integer data type. We will discuss strings and integers later in the lesson.
Data types
Along with the name of variables, their sizes may vary too. In most programming languages (like C++ and Java) you can use different data types for your variables. PHP, however, does not have explicit type definitions, but the type of a variable is determined by the type of the value that it is assigned, or by the type that it is cast to.
There are different data types for different purposes, i.e., null, boolean, integer, float, string, object, resource, and an array. Here is a brief overview of the types. For a detailed documentation and examples, see the PHP documentation.
Null
Null can be assigned to any variable. It represents a variable with no value.
Assigning null to a variable invalidates it and its value is undefined or void if it’s used. The variable is cleared from memory and deleted by the garbage collector.
Boolean
This is the simplest data type with only two possible values, i.e., true and false.
Booleans can be used to control the flow of code. Take a look at the code snippet below as an example. In case you’re not familiar with the given syntax you can learn more about it in a later lesson regarding if-else Statement:
Integer
An integer is a positive or negative number. It can be used with any number base (i.e., decimal, hexadecimal, octal, etc.). The size of an integer is platform-dependent.
PHP does not support unsigned integers. This means an integer can be positive or negative as each integer will contain information about its positivity or negativity. Consider the following code widget:
Float
Floating point numbers, “doubles” or simply called “floats” are decimal numbers.
Array
An array is like a list of values. The simplest form of an array is indexed by an integer, and ordered by the index, with the first element lying at index 0.
Arrays can also associate a key other than an integer index to a value. In PHP, all arrays are associative arrays behind the scenes, but when we refer to an associative array explicitly, we usually mean one that contains one or more keys that aren’t integers.
We will explore arrays in more detail in the chapter, Arrays in PHP.
String
A string is like an array of characters. Like an array, a string can be indexed to return its individual characters:
We will explore strings in more detail in the chapter, Strings
This whole process of declaring variables and storing data in these variables is summed up in the following figure:
Before moving to the next lesson, please answer the following question.