Datatype

PHP Datatype

PHP datatype are the type of data who store in variable.

supports total eight primitive data types:
  1. String
  2. Integer
  3. Float
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource

1.String:-

String holds letters or any alphabets, numbers, and even special characters.The strings can also be written within single quotes but it will be treated differently while printing variables.

Example

<?php
$Fruits = "Apple"; //both single and double quote statements are different
echo "$Fruits";
echo "</br>";
echo '$Fruits';
?>

output

Apple
$Fruits

2.Integer:-

Integer means numeric data with a negative or positive sign.Integers are whole numbers, without a decimal point and fractional number.Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation.

Example

<?php
$x=89;
echo"$x";
?>

Output

89

3.Float:-

float data type is a number with a decimal point or fractional number.

Example

<?php
$x=89.5;
echo"$x";
var_dump($x);
?>

Output

89.5
Float

4.Boolean:-

Boolean data type hold only two values, either TRUE or FALSE.If the condition is correct, it returns TRUE otherwise FALSE

Example

<?php
if (true)
echo "This condition is true.";
if (false)
echo "This condition is false.";
?>

Output

This condition is true.

5.Array

An array stores multiple values in one single variable.An php array is formally defined as an indexed collection of data values. Each index (also known a of an array is unique and references a corresponding value.

Example

<?php
$fruits=array("apple","banana","grapes");
print_r($fruits); ?>

output

Array ( [0] => apple [1] => banana [2] => orange [3] => )

6.Object

PHP Objects data type are the instances of user-defined classes that can store both values and functions.Objects are created based on this template via the new keyword.

example

>?php class pathshala{
methods
function show_greeting(){
return $this->str;
}
}
public $str = "Hello World!";
// $message = new pathshala;
var_dump($message);
?>

Output

object(pathshala)#1 (1) { ["str"]=> string(12) "Hello World!" }

7.Null

A Null data type is a variable without any data. php null data type isn represent empty variables in PHP.

Example

<?php
$a = NULL;
var_dump($a); ?>

Example

NULL

8.Resource

Resources in PHP are not an exact data type.It use to store references to some function call or to external PHP resources.

Example

<?php
$con = mysqli_connect("localhost","root"," ","users");
?>