July 10, 2017

Core PHP: Data Types (2.3)

PHP Float

A float, or floating point number, is a number that includes a decimal point.
<?php
  $x = 42.168;
?>

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE.
<?php
  $x = true; $y = false;
?>

Booleans are often used in conditional testing, which will be covered later on in the course.

Most of the data types can be used in combination with one another. In this example, string and integer are put together to determine the sum of two numbers.
<?php
  $str = "10";
  $int = 20;
  $sum = $str + $int;
  echo ($sum);

  // Outputs 30
?>

PHP automatically converts each variable to the correct data type, according to its value. This is why the variable $str is treated as a number in the addition.

No comments:

Post a Comment