PHP data type

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

yes  Boolean - true or false

yes  Integer- Integer

yes  String - Character set

yes  Float - Decimal numbers

yes  Array - Variable for storing multiple values

yes  Object - User-defined data type

yes  NULL - Empty variable

yes  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:

 yes Integer must have at least one digit.
yes Integer cannot contain decimal numbers.
yes Integer can be positive or negative.
yes Integer can be identified by three formats:
       yes decimal (10-based)
       yes hexadecimal (18 based - starts with 0x)
      yes 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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         $thisYear = 2021;
  12.         var_dump($thisYear);
  13.     ?>
  14. </body>
  15. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         $string1 = "Hello Bangladesh";
  12.         $string2 = "Hello Only Learn 24";
  13.         echo $string1;
  14.         echo "<br>";
  15.         echo $string2;
  16.     ?>
  17. </body>
  18. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         $number = 8.888;
  12.         var_dump($number);
  13.     ?>
  14. </body>
  15. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         $onlyLearn24 = array("HTML","CSS","Javascript");
  12.         var_dump($onlyLearn24);
  13.     ?>
  14. </body>
  15. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         // Class created
  12.         class Subject{
  13.             public $course = "PHP";
  14.         }
  15.         // Create an object
  16.         $learn = new Subject();
  17.         // Display the object's properties
  18.         echo $learn->course;
  19.     ?>
  20. </body>
  21. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php
  11.         $variable = null;
  12.         var_dump($variable);
  13.     ?>
  14. </body>
  15. </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.......