PHP Data Types

php-data-types-programming-pot
••• PHP Data Types

In this tutorial, we learn about PHP Data Types and how to use them effectively in the web application and web development. The data type specifies the amount of memory that allocates to the variable associated with it. In addition, the data type determines the operations that you can perform on it.

PHP Data Types are as follow:


PHP supports total eight primitive PHP Data Types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These data types are used to construct variables. Now let’s discuss each one of them in details.


PHP Data Types
Data Type Description Examples
Integer  whole number 6, 34, -45
Float (Double)
floating point numbers – also called double 6.6, 37.6, -34.553
String A string is a sequence of characters “Programming Pot”,

“1234@pqr”

Boolean Either true or false true, false
Array Can hold multiple values indexed by numbers or strings array(“php”,”java”,”python”);
Object stores data and information on how to process that data. see below
NULL Can only contain the value null, which means “no value” $var = NULL; 
Resource A resource is a special variable, holding a reference to an external resource. see below

1 ) PHP Integers


Integers are whole numbers, like 1, 12, and 256. The range of acceptable values varies according to the details of your platform but typically extends from -2,147,483,648 to +2,147,483,647. Specifically, the range is equivalent to the range of the long data type of your C compiler. Unfortunately, the C standard doesn’t specify what range that long type should have, so on some systems, you might see a different integer range.

It is one of the main PHP Data Types which is very useful for all another programming language also.

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 = 66; 
     echo $x; 
   // Output
     66
?>

Since PHP 5.4+ you can also specify integers in binary (base 2) notation. To use binary notation precede the number with 0b (e.g. $var = 0b11111111;).

2 ) PHP Floating Point Numbers or Doubles


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

Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) are decimal or fractional numbers, as demonstrated in the example below.

# Example:

<?php
 $x = 10.365;
 echo $x;
?>

3 ) PHP Strings


Because strings are so common in web applications, PHP includes core-level support for creating and manipulating strings. A string is a sequence of characters of arbitrary length. String literals are delimited by either single or double quote.

A string can hold letters, numbers, and special characters and it can be as large as up to 2GB (2147483647 bytes maximum). The simplest way to specify a string is to enclose it in single quotes (e.g. ‘Hello world!’), however, you can also use double quotes (“Hello world!”).

# Example:

<?php
  $x = "Hello world!";
  $y = 'Hello world!';

  echo $x;
  echo "<br>"; 
  echo $y;
?>

4 ) PHP Booleans


A Boolean value represents a “truth value”—it says whether something is true or not. Like most programming languages, PHP defines some values as true and others as false. Truth and falseness determine the outcome of conditional code such as:

if ($alive) { ... }

In PHP, the following values are false:

  1. The keyword false
  2. The integer 0
  3. The floating-point value 0.0
  4. The empty string ("") and the string "0"
  5. An array with zero elements
  6. An object with no values or functions
  7. The NULL value

# Example:

<?php
  $x = true;
  $y = false;
?>

5 ) PHP Arrays


An array holds a group of values, which you can identify by position (a number, with zero being the first position) or some identifying name (a string):

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example, a set of country or city names.

An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.

# Example:

<?php
  $programmingpot = array("Java","PHP","Python");
  var_dump($programmingpot );
?>

6 ) PHP Objects


PHP supports object-oriented programming (OOP). OOP promotes clean modular design, simplifies debugging and maintenance, and assists with code reuse.

Classes are the unit of object-oriented design. A class is a definition of a structure that contains properties (variables) and methods (functions). Classes are defined with the class keyword:

An object is a data type that not only allows storing data but also information on, how to process that data. An object is a specific instance of a class which serve as templates for objects. Objects are created based on this template via the new keyword.

Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

Here’s a simple example of a class definition followed by the object creation.

# Example:

<?php
  // Class definition
  class greeting{
    // properties
    public $string = "Hello World!";
    
    // methods
    function show_greeting(){
        return $this->string;
    }
}
  // Create object from class
  $message = new greeting;
  var_dump($message);
?>

7 ) PHP NULL


There’s only one value of the NULL data type. That value is available through the case-insensitive keyword NULL.

# Example:

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

  $b = "Hello World!";
  $b = NULL;
  var_dump($b);
?>

8 ) PHP Resources


A resource is a special variable, holding a reference to an external resource.

Resource variables typically hold special handlers to opened files and database connections.

Many modules provide several functions for dealing with the outside world. For example, every database extension has at least a function to connect to the database, a function to send a query to the database, and a function to close the connection to the database. Because you can have multiple database connections open at once, the connect function gives you something by which to identify that connection when you call the query and close functions: a resource.

Resources are really integers under the surface. Their main benefit is that they’re garbage collected when no longer in use. When the last reference to a resource value goes away, the extension that created the resource is called to free any memory, close any connection, etc. for that resource:

# Example:

<?php
  $res = database_connect( );   // fictitious function
  database_query($res);
  $res = "boo";                // database connection automatically closed
?>

These all are the PHP Data Types which are used in the php. For More PHP Data Types go through the PHP official website PHP.NET