1 of 12

Web Technologies

PHP Data Types

Smt.M.Jeevana Sujitha

Assistant Professor

Department of Computer Science and Engineering

SRKR Engineering College, Bhimavaram, A.P. - 534204

2 of 12

OBJECTIVES

The Objectives of this lecture are

  • To learn about PHP data types.

3 of 12

PHP Data Types

Data Type:

  • It represents the nature of data.
  • PHP supports the following data types.
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

4 of 12

PHP Data Types

String:

  • A string is a collection of characters.
  • A string can be any text inside quotes. Example:

<?php

$x = "Hello world!";

$y = 'Hello world!'; echo $x;

echo "<br>"; echo $y;

?>

5 of 12

PHP Data Types

Integer:

  • An integer data type is a non-decimal number between -

2,147,483,648 and 2,147,483,647.

Rules for Integers:

  • An integer must have at least one digit.
  • An integer must not have a decimal point.
  • An integer can be either positive or negative.
  • Integers can be specified in: decimal (base 10), hexadecimal

(base 16), octal (base 8), or binary (base 2) notation.

Example:

<?php

$x = 5985;

var_dump($x);

?>

6 of 12

PHP Data Types

PHP Float:

  • A float (floating point number) is a number with a decimal point or a number in exponential form.

Example:

<?php

$x = 10.365;

var_dump($x);

?>

7 of 12

PHP Data Types

PHP Boolean:

  • A Boolean represents two possible states: TRUE or FALSE.
  • Booleans are often used in conditional testing.

Example:

<?php

$x = true;

$y = false;

?>

8 of 12

PHP Data Types

PHP Array:

  • An array stores multiple values in one single variable.

Example:

<?php

$cars = array("Volvo","BMW","Toyota"); var_dump($cars);

?>

9 of 12

PHP Data Types

PHP Object:

  • An object is a data type which stores data and information on how to process that data.
  • In PHP, an object must be explicitly declared.
  • First we must declare a class of object.
  • For this, we use the class keyword.
  • A class is a structure that can contain properties and methods:

10 of 12

PHP Data Types

PHP Null:

  • Null is a special data type which can have only one value: NULL.
  • A variable of data type NULL is a variable that has no value assigned to it.
  • If a variable is created without a value, it is automatically assigned a value of NULL.

Example:

<?php

$x = "Hello world!";

$x = null; var_dump($x);

?>

11 of 12

PHP Data Types

PHP Resource:

  • The special resource type is not an actual data type.
  • It is the storing of a reference to functions and resources external to PHP.
  • A common example of using the resource data type is a database call.

12 of 12

THANK YOU