PHP-is a Loosely Typed Language

php, core php, gaurav dixit, gaurav
  •  Typed Languages
    • Strongly typed and weakly typed
      • C++, Java, are strongly typed with predefined type of data
      • PHP is weakly typed- it is only important that a variable exists, not whats its type is
  • This code sample shows each variable being defined by the type of data that goes into it - these variables were not defined earlier 
1:  $price=10;  
2: $tax=$price*.8;
3: $shipping=4;
4: $total_price=($price+$tax+$shipping);

  • In PHP, the type of a variable is set by how the variable is used. If you create a variable named address, but put a float value in it, it is treated as a float rather than the string that you may have intended.

  • Type checking allows you to control what kind of data go into variables (a string can't be entered into a numeric variable, and more importantly you won't end up multiplying numbers that resulted from converting a string to a number when that was not your intent.

  • A strongly typed language can help avoid hard to find mistakes. A loosely-typed language one lets you quickly create variables intended for specific purposes and narrow scope.

Post a Comment

Previous Post Next Post