Variables and Data Types in PHP

Continued from Understanding Variables:

PHP variables must also conform to a set of rules. Variable names must start with a dollar sign. They can contain any combination of strings, numbers, and underscores, but the first character after the dollar sign cannot be a number. PHP variables are also case-sensitive.

PHP variables don’t need an initial value when they are created, and they don’t need to be declared a specific data type either.

PHP has several built-in data types:

  • boolean – a data type with only two possible values: true or false
  • integer – any positive or negative whole number, or a zero
  • float – numbers that are too large or too small to be represented as integers
  • string – a series of alphanumeric characters, numbers, and punctuation marks
  • array – a data type which can hold several different values
  • object – built-in or user-defined classes of data; a collection of properties or attributes
  • resource – a variable holding a reference to an external resource, like a MySQL database
  • NULL – a special data value that has no value and means nothing; not the same as zero

When using integers, a leading 0 ( zero ) is used to specify that the integer is octal, and a leading 0x or 0X is used to specify hexadecimal. A floating point number can contain either a decimal or an “e” to represent “ten to the power of” in scientific notation, or both.

Note: ActionScript and JavaScript also use arrays, but they aren’t listed as a basic data type in the official documentation, as far as I’ve been able to discover, while arrays are listed as one of the eight main data types supported by PHP. While most of us just use arrays without considering whether they are a data type or not, I have endeavored to follow the various documentation in these articles. I apologize for any confusion this may cause.