PHP data type
PHP data type:
Data type
Variables can hold different types of data and different tasks can be performed through different types of data.
PHP supports the following data types
Boolean - true or false
Integer- Integer
String - Character set
Float - Decimal numbers
Array - Variable for storing multiple values
Object - User-defined data type
NULL - Empty variable
Resource - Identifies or refers to external functions or resources
PHP Boolean:
Boolean provides only two values. E.g. true or false.
Boolean data is often used to verify conditions. Condition verification is discussed in the next chapter.
PHP Integer:
The integer is any integer between -2,147,483,648 and 2,147,483,647.
Integer's rules:
Integer must have at least one digit.
Integer cannot contain decimal numbers.
Integer can be positive or negative.
Integer can be identified by three formats:
decimal (10-based)
hexadecimal (18 based - starts with 0x)
octal (৮-based - starts with 0)
In the following example $year is integer and PHP var_dump () function is used to know its data type and value:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $thisYear = 2021;
- var_dump($thisYear);
- ?>
- </body>
- </html>
Output:
int(2021)
PHP String:
A string is a sequence of letters. Such as- "Hello Bangladesh!".
Any text used in quotation marks is a string. Strings can be placed in single or double quotations
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $string1 = "Hello Bangladesh";
- $string2 = "Hello Only Learn 24";
- echo $string1;
- echo "<br>";
- echo $string2;
- ?>
- </body>
- </html>
Output:
Hello Only Learn 24
Hello Bangladesh
PHP Float:
Float is a decimal number. It can be written in decimal or exponential (ex) form.
In the following example, that $number is float and the PHP var_dump() function is used to determine its data type and value:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $number = 8.888;
- var_dump($number);
- ?>
- </body>
- </html>
Output:
float(9.999)
PHP Array:
Array is a variable that allows you to store more than one value at a time.
In the following example $satt is an array variable and the PHP var_dump() function is used to know its data type and value:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $onlyLearn24 = array("HTML","CSS","Javascript");
- var_dump($onlyLearn24);
- ?>
- </body>
- </html>
Output:
array(3) { [0]=> string(4) "HTML" [1]=> string(3) "CSS" [2]=> string(10) "Javascript" }
The next chapter discusses Array in detail.
PHP Object:
Object is a data type that contains not only data, but also information on how to process that data.
Object data type is different from all other data types. You must use the new keyword to declare an object in PHP.
For Object Data, we must first declare the class. You have to use the class keyword to announce the class. A class is a type of template or structure that can contain the object's properties and methods:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- // Class created
- class Subject{
- public $course = "PHP";
- }
- // Create an object
- $learn = new Subject();
- // Display the object's properties
- echo $learn->course;
- ?>
- </body>
- </html>
Output:
PHP
The next chapter discusses Object in detail.
PHP NULL:
Null is a special type of data type that has only one value. E.g.- NULL.
Variables in which no value is assigned have data type NULL.
Note: If a variable is declared without a value, its value is automatically NULL or empty.
Variable values can be left blank by assigning NULL.
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $variable = null;
- var_dump($variable);
- ?>
- </body>
- </html>
Output:
NULL
PHP Resource:
PHP is a special type of data type. Resource is not an actual data type. It actually stores the reference of the function or external PHP resource.
A common example of a resource data type is database calling.
Thank You.......
shelleyakter
wow nice post