Explain about Type Juggling in PHP?

Answer Posted / rekha_sri

In PHP there is no need to declare the variable with
explicit type declaration. Variable type is specify by the
context.

For example, if we assign the integer value into variable,it
will be integer variable and if we assign the string value
into variable it will be string.

Example:
--------
<?php
$a=10;
$b="string";
echo "$a"; // 10
echo "$b"; // String
?>

If we want to force the variable type into different type we
can do the type casting.

Type casting example:
---------------------
<?php
$one = 10; // $one is an integer
$two = (boolean) $one; // $two is a boolean
?>

In PHP we are having function called settype() for setting
the particular variable type.

settype() example:
------------------
<?php
$first = "10rose"; // string
$second = true; // boolean

settype($first, "integer"); // $first is now 10 (integer)
settype($second, "string"); // $second is now "1" (string)
?>

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is polymorphism in oop php?

720


Which is incorrect with respect to separating php code and html?

741


Tell me how can we connect to a mysql database from a php script?

737


What is rest api in php?

700


What is the difference between print() and echo() in PHP?

803


How do you connet mysql database with php?

776


What are form input html tags?

726


What is difference between session and cookies in php?

690


Why do we use interface in php?

710


What are the variables in php?

788


What are the string functions in php?

729


How do I check if a given variable is empty?

699


Write a program in php to reverse a number?

727


How to receive a cookie from the browser?

753


Tell us why did you choose this particular career path?

702