PHP

Data Types

PHP / Data Types

Data Types

Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • string (text values) The string data type is used to store text
  • int (whole numbers) The int (integer) data type is used to store whole numbers (numbers without decimal points).
  • float (decimal numbers) the float data type (also called floating-point or double) is used to store numbers with decimal points or numbers written in exponential form. 
  • bool (true or false)  The boolean data type is a primitive data type that represents only two possible states: true or false. It is mainly used to control the flow of a program by making decisions based on conditions.
  • array (multiple values) The array data type is used to store multiple values in a single variable.
  • object (stores data as objects) The object data type is used to store data and information in the form of objects, which are created using classes.
  • null (empty variable) The NULL data type is a special type used to represent a variable with no value.
  • resource (references external resources) The resource data type is a special type used to store references to external resources.


var_dump() is used in PHP to show the value and data type of a variable.
 

Example

// String data type
$name = "Rahul";

// Integer data type
$age = 20;

// Float (decimal) data type
$price = 99.99;

// Boolean data type
$isLogin = true;

// Array data type
$colors = array("Red", "Green", "Blue");

// Object data type
class Car {
   function drive() {
       echo "Car is moving";
   }
}

// Creating object of Car class
$car = new Car();

// NULL data type
$x = NULL;

// Displaying values with var_dump() for checking type and value
var_dump($name);
var_dump($age);
var_dump($price);
var_dump($isLogin);
var_dump($colors);
var_dump($car);
var_dump($x);

?>

Technology
PHP
want to connect with us ?
Contact Us