PHP Data Types

PHP Data Types-

PHP Data Types, Variables can store data of different types, PHP supports 8 primitive data types that can be categorized further into 3 types:
PHP Data Types image

  1. Scalar Types
  2. Compound Types
  3. Special Types

PHP Data Types: Scalar Types
There are 4 scalar data types in PHP.

  1. string
  2. float
  3. integer
  4. boolean

PHP Data Types: Compound Types

There are 2 compound data types in PHP.

  1. object
  2. array

Special Types
There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP String
a string is a sequence of characters, like “Wellcome Tutorial Scan” and can be used singly or double quotes

Example:-

<?php 
$x = "Welcome Tutorial Scan!";
$y = 'Welcome Tutorial Scan!';
echo $x;
echo "<br>"; 
echo $y;
?>

Output:

Welcome Tutorial Scan!
Welcome Tutorial Scan!

PHP Integer

  • PHP integer must have at least one digit
  • The integer must not have a decimal point
  • it integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based – prefixed with 0)

Example:-

<?php 
$x = 3456;
var_dump($x);
?>

Output:-

int(3456)

PHP Float

A float is a number with a decimal point or a number in exponential form.

$x is a float. The PHP var_dump() function returns the data type and value:

Example:-

<?php 
$x = 23.542;
var_dump($x);
?>

Output:-

float(23.542)

PHP Boolean-
A Boolean represents always two possible conditions: TRUE or FALSE.

$x = true;
$y = false;

PHP Array

An array stores multiple values in one single variable.

In the following example, $watch is an array. The PHP var_dump() function returns the data type and value:

Example:-

<?php 
$watch = array("HMT","Reebok","Titen");
var_dump($watch);
?>

Output:-

array(3) { [0]=> string(3) "HMT" [1]=> string(6) "Reebok" [2]=> string(5) "Titen" }

 

 

Resource
PHP.net