PHP Data Types Tutorial

PHP Data Types Tutorial.What kind of information do you want to store in a variable, you define it by using data types. For example, if you want to store a number or you want to store a character, it shows the compiler through the data types. PHP has 8 types of data types. There are 4 scalar types, 2 are compound types and 2 are special types.

Scalar types

  • Integer
  • String
  • Boolean
  • Float

Compound types

  • Array
  • Object

Special types

  • Resource
  • NULL

Scalar data types

Scalar data types are also called base types. These data types provide a programming language by default. Such data types hold a single value. Such as int, char, float, boolean etc.

PHP Integer

Integers are used to store a number. Integers always store the whole number.

  • 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 three formats: decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based – prefixed with 0)

Example

<?php
$n =30;
var_dump($n);
?>

PHP String

A string is a sequence of characters. In PHP, you can create the string types of variables in this way.

Example

<?php
$s = "Hello";
$a = 'Hii';

echo $s."<br>";
echo $a;
?>

PHP Boolean

The variables of Boolean types store truth values. You can store true and false one of the values ​​boolean type variables.

Example

$a = true;
$b = false;

PHP Float

Variables of float types are used to store decimal numbers. In PHP, you can store floating point variables this way.

Example

<?php
$n =30.45;
var_dump($n);
?>

Compound Data Types

Compound data types are those data types that you create in your program while using the programming language. Such as arrays, objects, structure, linked-lists, queue, stack etc.

PHP Array

The array in PHP is like a map. You store values ​​by keys. You can also use arrays in PHP as a list and you can also use it as a hash-table.

In PHP the array you can create in this way.

Example

<?php
$name = array("Ram","Shyam","Rohan");
?>

PHP Object

In PHP, you can also create object types. To create Object types you must first create a class. Creating classes and objects in PHP is as easy as any other programming language.

Example

<?php
class Student {
    function Student() {
        $this->name = "Ram";
    }

// create an object
$object = new Student();

// show object properties
echo $object->name;
}
?>

Special Data Types

Special Understanding The PHP Data Types Tutorial For Beginners are those data types that are made for one particular language. PHP’s special data types are being given below.

PHP Resource

A resource is a special type that holds external resources. Such as the database connection or files that you open via PHP code.

PHP NULL

If you have created a variable but you do not want to initially store a value store then you can store NULL in it. NULL means that there is no value of that variable.

Example

<?php
$name = NULL;
?>