PHP and Datatypes

From Wiki

Jump to: navigation, search


This article is a stub. You can help us by expanding it.

Contents

Integers

Integers are the simplest type-they correspond to simple whole numbers, both positive and negative. Integers can be assigned to variables, or they can be used in expressions, like so:

$intVariable = 12345;
$anotherInt = -12345 + 12345; // will equal zero

Integers can actually be read in three formats, which correspond to bases: decimal (base 10, default), octal (base 8), and hexadecimal (base 16).

  • Decimal format is the default,
  • Octal integers are specified with a leading 0, and
  • hexadecimal have a leading 0x.

Any of the formats can be preceded by a - sign to make the integer negative.

<?php
 $integer_10 = 1000;
 $integer_8 = -01000;
 $integer_16 = 0x1000;
 print("integer_10: $integer_10<br/>");
 print("integer_8: $integer_8<br/>");
 print("integer_16: $integer_16<br/>");
?>

yields the browser output:

integer_10: 1000
integer_8: -512
integer_16: 4096

The read format affects only how the integer is converted as it is read-the value stored in $integer_8 does not remember that it was originally written in base 8. Internally, of course, these numbers are represented in binary format; we see them in their base 10 conversion in the preceding output because that is the default for printing and incorporating int variables into strings.

Floating-Point Numbers

A floating-point number is called a double. Examples are:

$firstDouble = 123.456;
$secondDouble = 0.456
$evenDouble = 2.0;
$scientificNotationDouble = 2.8e16;

As you probably expect, the fact that $evenDouble is a "round" number does not make it an integer. Integers and doubles are stored in different underlying formats, and the result of:

$five = $evenDouble + 3;

is a double, not an integer, even if it prints as 5. By default, doubles print with the minimum number of decimal places needed. The following are legal doubles:

$smallPositive = 0.12345;
$smallNegative = -.12345
$evenDouble = 2.00000;
$stillDouble = 2.;

Booleans

Boolean operators associate left to right, and they short-circuit, meaning that they do not even evaluate their second argument if their truth value is unambiguous from their first argument. If a non Boolean value appears in a Boolean context and it not store actually a Boolean, the following rules determine the "truth" of such a value:

  1. If the value is a number, it is false if exactly equal to zero and true otherwise.
  2. If the value is a string, it is false if the string is empty (has zero characters) or is the string <ode>"0"</code>, and is true otherwise.
  3. Values of type NULL are always false.
  4. If the value is a compound type (an array or an object), it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.
  5. Valid resources are true (although some functions that return resources when they are successful will return false when unsuccessful).
$true_num = 3 + 0.14159;
$true_str = "Tried and true";
$true_array[49] = "An array element";
$false_array = array();
$false_null = NULL;
$false_num = 999 – 999;
$false_str = ""; // a string zero characters long

Although Rule 1 implies that the double 0.0 converts to a false Boolean value, it is dangerous to use floating-point expressions as Boolean expressions, due to possible rounding errors.

Boolean Operators

Strings

The special value NULL

To give a variable the NULL value, simply assign:

$myNull = NULL;

The intended meaning of NULL is the lack of a value.

A variable that has been assigned NULL has the following properties:

  • It evaluates to false in a Boolean context.
  • It returns false when tested with isset(). (No other type has this property.)
  • PHP will not print warnings if you pass the variable to functions and back again, whereas passing a variable that has never been set will sometimes produce warnings.

The NULL value is best used for situations where you want a variable not to have a value, intentionally, and you want to make it clear to both a reader of your code and to PHP that this is what you want. The latter point is particularly relevant when passing variables to functions.

=== Comparison Operators ===
Personal tools