PHP Datatypes

Today we will learn PHP Datatypes. PHP supports the following Datatypes:

  • String
  • Integer
  • Float (floating-point numbers – also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

To create variables, these data types are used. Now let’s discuss in depth each one of them.

PHP Strings

A sequences of characters is called string. A string can hold letters, numbers, and special characters.

 <?PHP

    $name = "Jone Doe";
    echo "Your name is $name"; // OUTPUT:Your name is Jone Doe

    $name = "Jone Doe";
    echo 'Your name is $name'; // OUTPUT:Your name is $name

    $name = "Jone Doe";
    echo "Your name is ". $name; // OUTPUT:Your name is Jone Doe

    $name = "Jone Doe";
    echo 'Your name is '. $name; // OUTPUT:Your name is Jone Doe

?>

Notice that there have two types of string formation, the first one is Double-quoted strings and the second one is single-quoted strings. In single-quoted strings, you can define a string using a single-quote only when you want to print exactly the same variable string. Within the single-quote, variable names act like a normal string. On the other hand, you can use double-quote to print strings with Variables. In this type of declaration, each variable will be replaced by its value.

PHP Integers

Integers are whole numbers, without a decimal point.

 <?PHP

    $number = 100;
    echo $number; //OUTPUT: 100

?>

PHP Floating Point Numbers or Doubles

Decimal or fractional numbers are called floating point number.

<?php

$number = 10.5;
echo $number; // OUTPUT: 10.5

?>

PHP Booleans

Booleans are like a switch it has only two possible values either 1 (true) or 0 (false).

<?php


$value1 = true;
echo $value1; // OUTPUT: 1

?>

PHP Arrays

An array is a variable that can hold more than one value at a time. It is useful to combine a sequence of similar things together, such as a collection of names of countries or towns.

<?php

$colors = array("Red", "Green", "Blue");
var_dump($colors);
echo "<br>";

?>
Read also:

PHP Objects

Object is a instance of a class. An object is a data type that not only allows information can be stored, but also how it can be processed.

<?php
// Class definition
class greeting{
    // instance variable
    public $msg = "Hello World!";
    
    // methods
    function show_greeting(){
        return $this->msg;
    }
}
 
// Create object from class
$message = new greeting;
 // new keyword is used to make object from class
var_dump($message);
?>
Read also:

PHP NULL

A special NULL value is used in PHP for the representation of empty variables. A NULL type variable is a variable without any data. The only possible value of form null is NULL.

<?php
$a = NULL;
var_dump($a);
echo "<br>";
 
$b = "Hello World!";
$b = NULL;
var_dump($b);
?>

PHP Resources

A resource is a special variable, keeping a relation to an external resource. Resource variables typically hold special handlers to opened files and database connections.

<?php
// Open a file for reading
$handle = fopen("note.txt", "r");
var_dump($handle);
echo "<br>";
 
// Connect to MySQL database server with default setting
$link = mysqli_connect("localhost", "root", "");
var_dump($link);
?>

Hope you got it!

Leave a Reply