data type, constant, variable

Data Types, Constants and Variables in PHP

Posted by

In the previous article, we have learned Print Statement, Code Blocks, and Commenting. In this article, we will learn the following topics:

Data Types:

The data types in PHP are used to hold various data types or values. There are 8 primitive data types which are further classified into 3 groups:

  • Scalar types(Integer, Float, Boolean, Strings)
  • Compound types(Array, Object)
  • Special types(Resource, null)

1. Scalar Types:

Scalar type holds a single type of data that can be integer or float or boolean or string.

a. Integer:

An integer data type is a whole number(……-3,-2,-1,0,1,2,3…….), it can be a positive or negative number. It can not be a fraction or decimal point.
Example:

Output:

b. Float:

A float is a number that has a decimal point for both positive and negative numbers. Various ways of representing float values are 2.34, 3.95, 6.77E+20, etc.
Example:

Output:

c. Boolean:

A Boolean holds two possible values: TRUE or FALSE. If the condition is a success then it will return true other return false.

d. Strings:

A string is a set of characters, such as “troposal.com”. A string can be any text inside a single(”) or double quotes(“”).
Example:

Output:

2. Compound types:

Compound data types allow holding multiple items of a different type in a single variable. PHP contains 2 compound data types: Array, Object.

a. Array:

An array holds multiple values in a single variable. You can check the output of an array by print_r(). In later, this will be discussed in detail.
Example:

Output:

b. Object:

Objects are user-defined class instances that can store both properties(values) and method(functions). Check the example for now and this will be discussed in detail in further articles.
Example:

Output:

3. Special data type:

There is two special data type in PHP: NULL and Resource:

1. NULL:

“NULL” is a variable that holds no value.
Example:

Output:
It will not show any output.

2. Resource:

As resource variables hold special handles for opening files, database connections, etc. In later, this will be discussed in detail.

Constants in PHP:

A constant is a name or an identifier that can assign a fixed value and can not be changed. The name of the constant can not be redefined.

Syntax:

name: The name or identifier of the constant.
value: The value of the constant.
case-insensitive: By default, this value is case sensitive. You can pass “true” for case insensitive.

Example:

Output:

Example:

Output:

Variables in PHP:

Variables, such as text strings, numbers, etc. are used to store data. It can be reused after declaring a variable and changed over the entire code.

Example:

During the execution of code, $var will hold the value “Test String”.

The following are the naming rules for the variable of PHP:

Example:

In PHP, variable names are case-sensitive. $var and $VAR are two different variables.

Leave a Reply

Your email address will not be published. Required fields are marked *