The Building Blocks / Page 4 | WebReference

The Building Blocks / Page 4


[previous]

The Building Blocks:
Data Types, Literals, Variables, and Constants

Boolean Literals

Boolean literals (introduced in PHP 4) are logical values that have only one of two values, true or false, both case insensitive. You can think of the values as yes or no, on or off, or 1 or 0. They are used to test whether a condition is true or false. When using numeric comparison and equality operators, the value true evaluates to 1 and false evaluates to the empty string (see Figure 4.8).

Example 4.6

Figure 4.8 True and false.

Special Data Types

Null. NULL represents "no value," meaning "nothing," not even an empty string or zero. It is a type of NULL. An uninitialized variable contains the value NULL. A variable can be assigned the value NULL, and if a variable has been unset, it is considered to be NULL.

Resource. A resource is a special variable, holding a reference to an external resource such as a database object or file handler. Resources are created and used by special functions. File and database resources are defined by the PHP interpreter and are only accessible by functions provided by the interpreter (see Chapter 11, "Files and Directories," and Chapter 15, "PHP and MySQL Integration).

The gettype() Function. The gettype() built-in function returns a string to identify the data type of its argument. The argument might be a variable, string, keyword, and so on. You can use the gettype() function to check whether or not a variable has been defined because if there is no value associated with the variable, the gettype() function returns NULL (see Figure 4.9).

Strings returned from the gettype() function include the following:

  • boolean (since PHP 4)
  • integer
  • double (for historical reasons double is returned in case of a float, and not simply float)
  • string
  • array
  • object
  • resource (since PHP 4)
  • NULL (since PHP 4)

Format

string gettype ( mixed var )

Examples:

Example 4.7

Figure 4.9 PHP data types. Output from Example 4.7.

This chapter is excerpted from the book titled, PHP and MySQL by Example, authored by Ellie Quigley. Copyright 2007 Pearson Education, Inc., published by Prentice Hall Professional, November, 2006. ISBN 0131875086.

[previous]

URL: