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

How to strip whitespace (or other characters) from the beginning and end of a string?

696


What is lazy loading in php?

707


Does https prevent csrf?

705


Can we run php on tomcat server?

793


What is the difference between a session and cookies?

716


Hello Friends,I am seeking for a job in php having 9 months. exp.Please suggest any company openings.

1916


Why and where do we use htaccess?

770


Why print_r is used in php?

738


What is final keyword in php?

752


How are sessions maintained?

730


How to remove the new line character from the end of a text line in php?

754


How to find a specific value in an array?

690


How can image properties be retrieved in php?

748


List some array functions in php?

701


What backslash character will match whitespace?

794