Broad Network


Some Scalar Data Types in PHP

PHP Data Types Simplified – Part 1

Forward: In this part of the series we look at integer, Boolean and Float data types in PHP.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is the first part of my series, PHP Data Types Simplified. In PHP a scalar data type is an integer or a float or a string or a Boolean Value. In this part of the series we look at integer, Boolean and Float data types in PHP. Of course, a scalar variable is a variable that holds a scalar. Everything said in this series is based on PHP 5.

You need basic knowledge in PHP in order to understand this series. If you do not have that prerequisite knowledge, then read the series I wrote in this blog whose first part is titled, Getting started with PHP. To reach that series, just type the title and my name Chrys in the Search Box of this page and click Search.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Integer
An integer is a number of the arithmetic set Z = {..., -3, -2, -1, 0, 1, 2, 3...}. You would declare and assign an integer value to a variable as in the following example:

    $var = 56;

An integer can be specified as a decimal notation (base 10), hexadecimal notation (base 16), or octal notation (base 8), optionally preceded by a sign (- or +). – is for negative numbers and + is for positive numbers. The above statement is base 10 specification.

To have an octal number, precede the number with zero as in

        $var = 0213;

To have a hexadecimal number, precede the number with zero and x as in

        $var = 0x1A;

The letters (A to F) of the hexadecimal number can be in lower or upper case.

Size of an Integer
The size of an integer is platform-dependent. The maximum size is about 2 billion.

Integer Overflow
If PHP encounters a number beyond the limits of the integer type, the number will be interpreted as a float instead.

Booleans
A Boolean Value is either TRUE or FALSE typed in either upper or lower case and should not be in quotes. Try the following program:

<?php

    $var0 = true;
    $var1 = false;
    $var2 = TRUE;
    $var3 = FALSE;

    echo $var0."<br />";
    echo $var1."<br />";
    echo $var2."<br />";
    echo $var3."<br />";

?>

If you tried the above program, you would have noticed that no value was displayed for false; do not worry about that.

Boolean values are usually the result of operators in conditionals like the if-condition. Read and try the following program:

<?php

    if (true)
        {
            echo "It is true indeed."."<br />";
        }

    if (5==5)
        {
            echo "5==5 as a condition with the operator, ==, returns TRUE."."<br />";
        }

?>

Equivalent Boolean Values
TRUE or FALSE are Boolean literals. There are other literals that are equivalent to TRUE or FALSE. That is what we look at in this section.

The following returned values are equivalent to FALSE:

- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string (""), and the string "0"
- an array with zero elements
- the special type NULL (including unset variables) – See later
- SimpleXML objects created from empty tags – See later

The following returned values are equivalent to TRUE:
- Any literal or data type that is not in the above list is equivalent to TRUE.

Floating-Point Number
A floating-point number is called a float, for short. In mathematics, a float is number with a decimal point. An example of a float is:

    253.65

In math, this same number can be written in standard form as, 2.5365 X 10 raised to the power 2. PHP also has several ways of typing this same number. That is, PHP has several ways of typing a float.

Typing a Float in PHP
You can type the above number in the following three ways:

253.65
2.5365e2
2.5365E2

The last two numbers are the way PHP writes her numbers in standard form. You are expected to type your PHP source code in a text editor or similar editor. The text editor does not offer you anyway of typing indices to figures. So in PHP, e or E in a number like the two above, means base 10. The figure to the right of e or E is the power to which 10 is raised. The figure to the left has all the digits and the decimal point of the number in question.

Read and try the following program:

<?php

    $var0 = 253.65;
    $var1 = 2.5365e2;
    $var2 = 2.5365E2;

    echo $var0."<br />";
    echo $var1."<br />";
    echo $var2."<br />";

?>

Maximum Size and Precision of a Float
This is what is in the PHP specification about the maximum size and precision of the float: “The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).”

We end here for this part of the series and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message