Monday, January 25, 2021
Home PHP PHP Lessons PHP Lesson 2: PHP Data Types and Variables

PHP Lesson 2: PHP Data Types and Variables

In this lesson will be talking about PHP Data Types and Variables  which is an important topic.

Data Type

Data types identifies the type of the data we are dealing with in our PHP programs ( Or in any other programming language. ).  PHP is supporting two data types, Scalar and Compound. Both are explained below.

Scalar Data Types: Scalar data types have only one value at a time. There are four scalar types in PHP which are int, float, strings and boolean.

Float and Int represent numeric values. Numeric values can be decimal, octal, hexadecimal and exponential. PHP support all of these values for different purposes.

Strings on the other hand seems to be text and it is the case but not exactly true in PHP. In PHP strings are binary data which may be text, content of an image and so any other binary data. But for simplicity at this point just consider Strings as text.

Boolean has only two value, TRUE ( true ) and FALSE ( false ).  1 is also considered is true and 0 is considered as false.


PHP variables are used to hold data. The data may be a result value of a mathematical operation, an integer number, an expression or text. PHP is a loosely typed language means there is no need to declare a variable before it is used. Declaring a variable means to define its data type that why type of data this variable can hold. A PHP variable can hold strings, integer number, float numbers etc etc .

There are some rules for PHP variables.

  • PHP variable starts with the $ sign followed by the variable name e.g. $results
  • PHP variable name may contain alpha-numeric characters and underscore e.g. $_res_90
  • PHP variable name cant be started with a number like $9abc ( Incorrect ). Variable name must start with an underscore or a character.
  • PHP variables are case sensitive, means $results and $Results both are different variables.
  • PHP variable names should not have spaces like $sum results . If such a variable name is needed, then replace the spaces with the underscore ” _ ” like $sum_results .

Look at the below example and see some variables.


$text = "This is a test text";

$number = 1452.45;


The PHP Lesson 2 : PHP Data Types and Variables ends here, and next lesson is about Arithmetic operations.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....