Variables in PHP

PHP is a dynamically typed language. As a consequence, you do not declare variables before you start using them. If you need to store a value into a variable, you simply name the variable and use an assignment operator to assign the value. Since you do not declare variables, you also do not specify a variable’s type. If you assign a string value to a variable, its type becomes a string. If you assign an integer to a variable, its type becomes an integer. If you reassign the value of a variable to a value with a different type, the variable’s type also changes.

Internally PHP supports several different types: Booleans, integers, floating point numbers, strings, arrays, and objects. The way that integers are represented may be platform dependent, but are usually 32-bit signed two’s complement integers, able to represent integers between− 2,147,483,648 and 2,147,483,647. Floating point numbers are also platform dependent, but are usually 64-bit double precision numbers defined by the IEEE 754 standard, providing about 16 digits of precision. Strings and single characters are the same thing in PHP. Strings are represented as sequences of characters from the extended ASCII text table which includes all characters in the range 0–255. PHP does not have native Unicode support for international characters.

To use a variable in PHP, you simply need to assign a value to a named variable identifier and the variable is in scope. Variable names always begin with a single dollar sign, $. The assignment operator is a single equal sign,= and is a right-to-left assignment. The variable that we wish to assign the value to appears on the left-hand-side while the value (literal, variable or expression) is on the right-hand-size. For example:

$numUnits = 42;
$costPerUnit = 32.79;
$firstInitial = "C";

Each assignment also implicitly changes the variable’s type. Each of the variables above becomes an integer, floating point number, and string respectively. Assignment statements are terminated by a semicolon like most executable statements in PHP. The identifier rules are fairly standard: a variable’s name can consist of lowercase and uppercase alphabetic characters, numbers, and underscores. You can also use the extended ASCII character set in variable names but it is not recommended (umlauts and other diacritics can easily be confused). Variable names are case sensitive. As previously mentioned, variable names must always begin with a dollar sign, $. Stylistically, we adopt the modern lower camel casing naming convention for variables in our code.

If you do not assign a value to a variable, that variable remains undefined or “unset.” Undefined variables are treated as null in PHP. The concept of “null” refers to uninitialized, undefined, empty, missing, or meaningless values. In PHP the keyword null is used which is case insensitive ( null , Null and NULL are all the same), but for consistency, we’ll use null . When null values are used in arithmetic expressions, null is treated as zero. So, (10 + null) is equal to 10. When null is used in the context of strings, it is treated as an empty string and ignored. When used in a Boolean expression or conditional, null is treated as false.

PHP also allows you to define constants: values that cannot be changed once set. To define a constant, you invoke a function named define and providing a name and value.

define("PI", 3.14159);
define("INSTITUTION", "University of Nebraska-Lincoln");
define("COST_PER_UNIT", 2.50);

Constant names are case sensitive. By convention, we use uppercase underscore casing. An attempt to redefine a constant value will raise a script warning, but will ultimately have no effect. When referring to constants later in the script, you use the constant’s name. You do not treat it as a string, nor do you use a dollar sign. For example: $area = $r * $r * PI;


Licenses and Attributions


Speak Your Mind