YouTip LogoYouTip

Php Datatypes

PHP Data Types |

-- Learning is not just about technology, but also about dreams!

PHP Tutorial

PHP Tutorial PHP Introduction PHP Installation PHP Syntax PHP Variables PHP echo/print PHP EOF(heredoc) PHP Data Types PHP Type Comparisons PHP Constants PHP String PHP Operators PHP If...Else PHP Switch PHP Arrays PHP Array Sorting PHP Superglobals PHP While Loop PHP For Loop PHP Functions PHP Magic Constants PHP Namespaces PHP OOP PHP Quiz

PHP Forms

PHP Forms PHP Form Validation PHP Form - Required Fields PHP Form - Validate Email and URL PHP Complete Form Example PHP $_GET Variable PHP $_POST Variable

PHP Advanced Tutorial

PHP Multidimensional Arrays PHP Date PHP Includes PHP Files PHP File Upload PHP Cookie PHP Session PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filters PHP Advanced Filters PHP JSON

PHP 7 New Features

PHP 7 New Features

PHP Database

PHP MySQL Introduction PHP MySQL Connect PHP MySQL Create Database PHP MySQL Create Table PHP MySQL Insert Data PHP MySQL Insert Multiple Data PHP MySQL Prepared Statements PHP MySQL Read Data PHP MySQL Where PHP MySQL Order By PHP MySQL Update PHP MySQL Delete PHP ODBC

PHP XML

XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX

AJAX Introduction AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

PHP Reference Manual

PHP Array PHP Calendar PHP cURL PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP PDO PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones PHP Image Processing PHP RESTful PHP PCRE PHP Available Functions PHP Composer

PHP EOF(heredoc) Usage Instructions

PHP Type Comparisons

Deep Dive

  • Web Service
  • Software
  • Web Design & Development
  • Web Services
  • Programming
  • Scripting Languages
  • Computer Science
  • Development Tools
  • Programming Languages
  • Scripts

PHP Data Types


PHP variables can store different types of data, and different data types can perform different actions.

PHP supports the following data types:

  • String (string)
  • Integer (integer)
  • Float (floating-point number)
  • Boolean (boolean)
  • Array (array)
  • Object (object)
  • NULL (null value)
  • Resource (resource type)

PHP String

A string is a sequence of characters, like "Hello world!".

You can place any text inside single or double quotes:

Example

<?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>

Try it Β»


PHP Integer

An integer is a number without a decimal point.

Integer rules:

  • An integer must have at least one digit (0-9)
  • An integer cannot contain commas or spaces
  • An integer has no decimal point
  • An integer can be positive or negative
  • An integer can be specified in three formats: decimal, hexadecimal (prefixed with 0x), or octal (prefixed with 0).

In the following example, we will test different numbers.

PHP var_dump() function returns the data type and value of a variable:

Example

<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; // negative number
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x);
?>

Try it Β»


PHP Float

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

In the following example, we will test different numbers. PHP var_dump() function returns the data type and value of a variable:

Example

<?php
$x = 10.365;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>

Try it Β»


PHP Boolean

A Boolean can be TRUE or FALSE.

$x = true;
$y = false;

Booleans are often used in conditional judgments. You will learn more about conditional control in the following chapters.


PHP Array

An array can store multiple values in a single variable.

In the following example, an array is created, and then the PHP var_dump() function is used to return the data type and value of the array:

Example

<?php
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
?>

Try it Β»

You will learn more about arrays in the following chapters.


PHP Object

Object data types can also be used to store data.

In PHP, an object must be declared.

First, you must declare a class object using the class keyword. A class is a structure that can contain properties and methods.

Then we define data types within the class, and then use the data types in the instantiated class:

Example

<?php
class Car {
    var $color;
    function __construct($color = "green") {
        $this->color = $color;
    }
    function what_color() {
        return $this->color;
    }
}
?>

Try it Β»

In the example above, the PHP keyword this is a pointer to the current object instance, not pointing to any other object or class.

You will learn more about objects in the following chapters.


PHP NULL Value

The NULL value indicates that a variable has no value. NULL is the value of the data type NULL.

The NULL value indicates whether a variable is empty. It can also be used to distinguish between empty data and NULL values.

You can clear a variable's data by setting its value to NULL:

Example

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>

Try it Β»


PHP Resource Type

PHP resource is a special variable that holds a reference to an external resource.

← Php ConstantsPhp Echo Print β†’