CopyPastehas never been so tasty!

Set variable type using settype () function in php

by anonymous

  • 0
  • 0
  • 0
83 views

 

 Orignal Source:http://codingvilla.com/set-variable-type-settype-function-php-single-article-573.aspx

Variable Types in php:

 

In php variable type is not defined at the time of declaration. When we assign a value to a variable type can be automatically defined through the value. But we can also defined type of variable in php.

 

Set variable type using settype () function in php:

 

To set a type of a variable in php we use settype () built in function of php. Settype () function set the type of a function. Settype function takes two parameters variable and type and set the type according to the given type.

 

 

Code:

 

 

<?php

$undecided = 3.14;

    print gettype( $undecided );

    print " -- $undecided<br />";

    settype( $undecided, string );

    print gettype( $undecided );

    print " -- $undecided<br />";

    settype( $undecided, int );

    print gettype( $undecided );

    print " -- $undecided<br />";

    settype( $undecided, double );

    print gettype( $undecided );

    print " -- $undecided<br />";

    settype( $undecided, bool );

    print gettype( $undecided );

    print " -- $undecided<br />";

?>

 

 

Above is a simple code to set different types of a variable using settype () function in php.

 

 

This simple article tells that how we can set the type of a variable using settype () function in php.

Add A Comment: