PHP Data Types Tutorial

php string, php data types

PHP DATA TYPES TUTORIAL

Data types are used to store the value of variable. PHP contain different data types to store different value of variable.

PHP can handle total eight primitive data types: Integer, Float or Floating point number, String, Boolean, Object, Array,,NULL and Resource.

To create variables in PHP, these data types are used to define the nature of the variable.

There are main 3 types and both types contain data types. Following is the list of data types…

  1. Scalar types (predefined)
  2. compound types (user-defined)
  3. special types

SCALAR TYPES :

Scalar data types contain only single value. There are four type of scalar data type.

It means predefined value type of variable.

  • String
  • Integer
  • Float
  • Boolean

Let us understand all the above scalar types in detail.

STRING :

– This is non numeric data type which holds the sequence of characters.

– It contain letter or any alphabet, numbers or special characters or may be all together.

– It’s used to store the array of character.

– String can be defined within single quotes or double quotes. Both are executed differently.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>

<?php 
   $str1 = "Hello world!";
   $str2 = 'Welcome to Demonuts!';

   echo $str1;
   echo "<br>"; 
   echo $str2;
?>

</body>
</html>

Below is the output of the above code:

Hello world!
Welcome to Demonuts!

INTEGER :

– This is numeric data type. Integers are whole numbers, without a decimal point (…, -2, -1, 0, 1, 2, …).

– Integer must have at least one digit.

– It contain only numbers without decimal points.

– Contain numeric value with positive or negative sign.

– It can be clarify in : Decimal (base 10), Octal(base 8), Hexadecimal(base 16), binary(base 2).

– It may be less than greater than or equal to zero.

– The range of Integer data type must be in between 2,147,483,648 and 2,147,483,647.

– In PHP var_dump() function returns the data type.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>

<?php  
   $a = 300395;
   var_dump($a);
?>  

</body>
</html>

Below is the output of the above code:

int(300395)

FLOAT :

– Float data type is also known as double data type.

– Float means floating point numbers. (For example, 0.1 , 2.3 , 3.7 etc.)

– It contain the number with decimal point or number in exponential form.

– It can hold fractional or decimal part with positive or negative sign.

– Mainly used in banking or financial sector for accuracy of currency.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>

<?php  
   $a = 0204.93;
   var_dump($a);
?>  

</body>
</html>

Below is the output of the above code:

float(204.93)

BOOLEAN :

– Boolean holds two value either TRUE(1) or FALSE(0).

– Boolean data type used with conditional statements. If the given condition is right then gives TRUE otherwise gives FALSE.

– It is the simple data type works like switch either ON or OFF.

– If a string is empty then in boolean data type it consider as false.

– NULL type value also consider as FALSE in boolean data type.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>

<?php  
   $x = true;
   $y = false;
   var_dump($x);
   echo "<br>";
   var_dump($y);
?>  

</body>
</html>

Below is the output of the above code:

bool(true)
bool(false)

COMPOUND TYPE

– It contain more than one value for single variable

– There are 2 compound data types.

  • Array
  • Object

ARRAY :

– It is compound data type

– Array stores multiple value in single variable.

EXAMPLE:

<!DOCTYPE html>
<html>
<body>

<?php  
   $language = array("C","C++","JAVA");
   var_dump($language);
?>  

</body>
</html>

Below is the output of the above code:

array(3) { [0]=> string(1) "C" [1]=> string(3) "C++" [2]=> string(4) "JAVA" }

OBJECT :

– Object is the data type which contain the information on how to process on data.

– It contain both values and functions.

– It is instance of user defined class

– Object can be explicitly declared in PHP.

– First we have to declare class of object. Here we use the class keyword.

EXAMPLE:

<!DOCTYPE html>
<html>
<body>

<?php
class Language {
    function Language() {
        $this->model = "PHP";
    }
}
// create an object
$topic = new Language();

// show object properties
echo $topic->model;
?>

</body>
</html>

Below is the output of the above code:

PHP

SPECIAL TYPES

It contain two data types

  • Null
  • Resource

NULL :

– defined variable with no value.

– Declaration for NULL value is always in capital latter because it is case sensitive.

– It contain NULL as a value.

EXAMPLE:

<!DOCTYPE html>
<html>
<body>

<?php
   $x = "welcome to demonuts";
   $x = null;
   var_dump($x);
?>

</body>
</html>

Below is the output of the above code:

NULL

RESOURCE :

– Resource data type is not exact data type.

– It is used to store some reference to some functions to other outside PHP scripts.

– Mainly use for database call. For example, consider a database call. This is an external resource.