Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 PHP Data type function, PHP has data type or not?


PHP is a web script and programming language both and every programming language has a data type, without using data type we can not create programs and allocate memory.


PHP is a loosely coupled script because datatype and variable have no dependency. means one variable can contain multiple types of data.

PHP code interpreter will automatically recognize data type according to assigned data.


for example, if we write


$a = 10;  10 is the integer type of data then it's datatype will be int.

$a = "10"  "10" is the string type of data because if we write data on a single quote or double quote then it will be String.


We can use the datatype function to differentiate data in PHP Script.


1) gettype():-  It return datatype of particular variable.

$a=10;

echo gettype($a);


2)settype():-   we can change datatype from one type to another.

<?php


$a=10;

echo gettype($a)."<br>";

$a="10";

settype($a,"integer");

echo gettype($a)."<br>";

$a=12.45;

echo gettype($a)."<br>";

$a = true;

echo gettype($a)."<br>";


?>


3) var_dump():-  It provide complete declaration of variable means it return type and values.

to test variable value we can use var_dump();


$a=10;

var_dump($a);

o/p int(10)


4) is_int(), is_float(), is_numeric(), is_string(), is_array():-


these all are predefined function is used to check variable is integer, float, number, string, or array.


Complete Example to Explain Data type function in PHP


<?php


$a=10;

echo gettype($a)."<br>";

$a="10";

settype($a,"integer");

echo gettype($a)."<br>";

$a=12.45;

echo gettype($a)."<br>";

$a = true;

echo gettype($a)."<br>";

$a=10;

var_dump($a);

if(is_int($a))

{

echo "integer";

}

else

{

echo "not int";

}

if(is_float($a))

{

echo "float";

}

else

{

echo "not float";

}

if(is_string($a))

{

echo "string";

}

else

{

echo "not string";

}


if(is_numeric($a))

{

echo "numeric";

}

else

{

echo "not numeric";

}

?>


Mock Interview session of PHP:-

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post