|
Data Types and Variables
This chapter describes:
- What are PHP data types.
- How to define variables, references, and variable variable names.
- How to define and use constants.
Data Types
PHP supports 8 data types:
- boolean: It has only two literal values: 'true' and 'false'.
- integer: It is stored as signed integers with 32 bits. Integer literals have 3 forms:
decimal, octal (prefixed with '0'), and hexadecimal (prefixed with '0x').
- float: It is stored as IEEE double floating point numbers with 64 bits. Float literals have
the standard floating point number forms.
- string: It is a sequence of 8-bit characters. String literals have 3 forms: single quoted,
double quoted, and heredoc syntax. These forms are very similar to Perl.
- array: It stores an ordered map of pairs of keys and values. This is quite different than
the array type used in other languages.
- object: It stores an instance of a class.
- resource: It represents an external resource like a file, or a database connection.
- null: It represents a status of a variable in which no value is assigned the variable.
Null type has only one literal value: 'null'.
Data Literals ....
PHP supports 8 data types:
- boolean: It has only two literal values: 'true' and 'false'.
- integer: It is stored as signed integers with 32 bits. Integer literals have 3 forms:
decimal, octal (prefixed with '0'), and hexadecimal (prefixed with '0x').
- float: It is stored as IEEE double floating point numbers with 64 bits. Float literals have
the standard floating point number forms.
- string: It is a sequence of 8-bit characters. String literals have 3 forms: single quoted,
double quoted, and heredoc syntax. These forms are very similar to Perl.
- array: It stores an ordered map of pairs of keys and values. This is quite different than
the array type used in other languages.
- object: It stores an instance of a class.
- resource: It represents an external resource like a file, or a database connection.
- null: It represents a status of a variable in which no value is assigned the variable.
Null type has only one literal value: 'null'.
Variables
Like Perl, a PHP variable name must be prefixed with a "$" sign.
PHP variables do not need declaration. Variable can be assigned with one type of data,
then re-assigned with another type of data.
Reference: PHP supports a reference concept, where a reference of a variable can
be expressed with the reference operator '&', and assigned to a new variable.
Once assigned with a reference, the new variable and the old variable become
aliases to each other. They are sharing the storage location in memory.
Variable Variable Name: PHP supports a dynamic variable name concept, where a variable
name can be expressed as a string expression in the form of ${string_expression}. If the string
expression is only a single variable, it can also be written as $$variable.
To show you some of the variable features, I wrote the following PHP script, VariableTest.php:
<?php # VariableTest.php
# Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
#
print "\nSimple assignments:\n";
$a = 777; # Assigning an integer
print " a = $a\n";
$b = 3.14; # Assigning a float
print " b = $b\n";
$c = "Hello"; # Assigning a string
print " c = $c\n";
$d = true;
print " d = $d\n";
#
print "\nSpecial assignments:\n";
$a = "Test"; # Re-assigning a string
print " a = $a\n";
$a = null; # Assigning no-value
print " a = $a\n";
$x = &$a; # Assigning a reference
$x = 888; # Assigning an integer to an alias.
print " a = $a\n";
#
print "\nVariable variable names:\n";
${'a'} = 999; # Variable variable name as a string literal
print " a = $a\n";
$y = 'a';
${$y} = 888; # Variable variable name as a string variable
print " a = $a\n";
$$y = 777;
print " a = $a\n";
?>
Here is the output:
Simple assignments:
a = 777
b = 3.14
c = Hello
d = 1
Special assignments:
a = Test
a =
a = 888
Variable variable names:
a = 999
a = 888
a = 777
As you can see from the output, the print statement seems to behave differently than other languages:
- The print statement prints boolean true as 1.
- The print statement prints null as nothing.
Constants
PHP supports constants with the following rules:
- Constants can be defined with the define("constant_name", value) function. Constant names
can not be redefined.
- Constant values can be referred by constant names.
|