PHP Data Types

Data Types In PHP

Hy Folks, Today we will learn Data Types in PHP. Data types are important in PHP because they tell the compiler which types of variable defined so that the Compiler can easily execute the programme. If you will not define a variable Data types so the programme will stop and give an error. In Php, you do not need to define the data type at the beginning of a variable but you have to remember some data types so that you can easily find out which type of data assigned to a value. Don't worry below I described the Important Data types in PHP.

In another language like c, c++, java you have to define the data type.


In PHP 

$data_type = "Hello Folks"; // String data type

 

In C or C++

int data_type =  123    // integer 


So, let's start 

PHP supports the following data types :

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • NULL

1 -   String

Any text which is written in single or double-quotes. if you assign a string value while integer or float if this is in double or single-quoted so it will be called a String. The compiler will treat it as a String. below is the Example of a string.


Example -


<?php 

    $a = '1.23' // single quotes 

    $b = "111" // double quotes 

    $c = "Hello folks" 

    echo $a;

    echo $b;

    echo $c;

    var_dump($a); // it will tell the data type with count of character 

     All are String Example

*Note if you want to see the type of variable use var_dump($a);

?>

 

2 - Integer


Any numeric value which have only digits not even decimal value. If value is negative or positive so it is also called Integer value.


Example

<?php

$negativeInteger = -123;
$postiveInteger = 321; echo $postiveInteger;
*Note if you want to see the type of variable use var_dump($negativeInteger);

?>

 

3 - Float

A float is a number with decimal point called float.


Example

<?php

$float_number = 2.345

echo $float_number;

*Note if you want to see the type of variable use var_dump($float_number);

?>

 

4 - Boolean

It is returns True or False. It is mostly used for check the condition or where we have to create some logic on the behalf of boolean value.


Example

<?php

$booleanA = true ;

$booleanB = false ;

if($booleanA){

        echo "condition is true";

}

if( !$booleanB ){ // here i used ! that mean Not

        echo "condition is false";

}

*Note if you want to see the type of variable use var_dump($booleanA);

?>

 

5 - Array

Array is collection of data. Mostly we use array when we need to store lots of data in single variable that's why we use Array beacuse array can store mulitple values but normal value can not store multiple value.


we can define array like  array() or [ ] ;


Example


<?php

// array()
$mobile = array("sony","samsung","nokia","vivo","oppo");

// OR 

// [ ]
$mobile = ["sony","samsung","nokia","vivo","oppo"];


var_dump($mobile);
// output
array(5) { [0]=> string(4) "sony" [1]=> string(7)

"samsung" [2]=> string(5) "nokia" [3]=> string(4)

"vivo" [4]=> string(4) "oppo"}

print_r($mobile);
// output
Array
(
[0] => sony
[1] => samsung
[2] => nokia
[3] => vivo
[4] => oppo
)

*Note if you want to see the type of variable use var_dump($mobile);

?>


6 - Object

An object is a data type which stores data and information. We have to declare a class of object for seeing the use of the object.

Class - is a structure it contains methods and properties.

Example -

<?php

// declare a class

class User{

        function abc(){

            return $this->name = "Short Tutorials";

}

}

// create a Object

$userObj = new User();

// call class method using object

echo $userObj->abc(); // Short Tutorials

*Note if you want to see the type of variable use var_dump($userObj);

?>

7 - NULL

It has only one value : NULL

If we do not assign a value to a variable so it will assign NULL by default

Example -

<?php 


     $a = "hello";

     $b = null;

    var_dumb($a)   // string(5) "hello"

    var dumb($b). // NULL


?>

Yeah! You have learned ЁЯШК

I hope you understood this and like this article. if you like it so, please give feedback in the comment section.

If any mistakes I made here, so please let me know and improve me.
If you have any query, feel free to ask me  ЁЯШК 


Recommended Post



Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post