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:
1 2 3 4 5 6 7 | <?php $a = 200; echo $a; echo "<br>"; $b = 300; echo $b; ?> |
Output:
1 2 | 200 300 |
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:
1 2 3 4 | <?php $f = 15.28; echo $f; ?> |
Output:
1 | 15.28 |
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:
1 2 3 4 | <?php $str = "troposal.com"; echo $str; ?> |
Output:
1 | troposal.com |
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:
1 2 3 4 | <?php $arr = array("John", "Troposal", "Site"); print_r($arr); ?> |
Output:
1 2 3 4 5 6 | Array ( [0] => John [1] => Troposal [2] => Site ) |
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:
1 2 3 4 5 6 7 8 9 10 | <?php class User { function getUser() { $userName = "John"; echo "User Name: " .$userName; } } $obj = new User(); $obj->getUser(); ?> |
Output:
1 | User Name: John |
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:
1 2 3 4 | <?php $var = NULL; echo $var; ?> |
Output:
It will not show any output.
1 | NULL |
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:
1 2 3 | <?php define(name, value, case-insensitive); ?> |
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:
1 2 3 4 | <?php define("SITEURL", "www.troposal.com"); echo SITEURL; ?> |
Output:
1 | www.troposal.com |
Example:
1 2 3 4 | <?php define("SITEURL", "www.troposal.com", TRUE); echo siteurl; ?> |
Output:
1 | www.troposal.com |
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:
1 2 3 | <?php $var = "Test String"; ?> |
During the execution of code, $var will hold the value “Test String”.
The following are the naming rules for the variable of PHP:
Example:
1 2 3 4 5 6 7 | <?php $var = 'Sample String'; //valid $Var = 'John'; //valid $4ab = 'Hi'; //This is invalid because of starts with a number $_abc = 'Hello'; //valid; $_6abc = 'troposal.com'; //valid ?> |
In PHP, variable names are case-sensitive. $var and $VAR are two different variables.