AGENDA:
1. What is type juggling in PHP?
2.Example of type juggling in PHP
3. What is type casting gin PHP?
4. Example of typecasting.
5. What is gettype() and settype() function in PHP?
6. Example of gettype() and settype() function.
1. Type Juggling in PHP
(Q. Define Type Juggling in PHP)
-Type juggling means dealing with a variable type. In PHP a variable type is the context in which it is used. If an integer value is assigned to a variable, it becomes an integer. If a string value is assigned to the variable, it becomes a String.
-PHP does not require (or it support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.
-PHP variable types may be modified without any explicit conversion process. This conversion process, whether explicit or implicit, is known as type juggling.
<?php
$a=1;#int
$b='2';#string
$c=$a+$b;#become int
echo "$c",$c;
?>
2. Type Casting in PHP
(Q. Describe Type Casting in PHP)
-Typecasting is a way to convert one data type variable into different data types. A type can be cast by inserting one of the casts in front of the variable
<?php $a=1;#int $b='2';#string $c=(int)$b;#variable b is assigned to int $a=(bool)$a;#variable a is asigned to boolean value echo var_dump($c); echo var_dump($a); ?>
Output: int(2) bool(true)
-PHP provides datatype function to get or set the data type of a variable or to check whether a variable is a particular datatype. Those functions are gettype(), settype().
-gettype() is used to check the data type of variable and settype() is used to change variable datatype.
<?php
$a="5";
echo gettype($a);
echo "<br>";
echo settype($a,'int');
echo "<br>",gettype($a);
?>
Output: string 1 integer
Comments
Post a Comment
If you have any query, please let us know