PHP - Introduction Loose Typing

Introduction

PHP is known as a loosely-typed language.

You don't need to worry about specifying the type of a variable when you declare it.

It converts a variable's data type automatically, depending on the context in which the variable is used.

For example, you can initialize a variable with an integer value.

Then add a float value to it, thereby turning it into a float; then join it onto a string value to produce a longer string.

PHP's loose typing makes variables very flexible.

PHP won't tell you if you accidentally pass around data of the wrong type.

For example, PHP will let you pass a floating - point value to a piece of code that expects to be working on an integer value.

Demo

<?php

   $a = 1;/*from  w  ww.  j  a v  a  2  s.  co m*/
   print($a . '\n');
   $a = 1.1;
   print($a . '\n');
   $a = "string";
   print($a . '\n');

?>

Result

Related Topic