Chapter 5. Variables and Constants

In this chapter, we examine the different variable types used in PHP, which to use and when, and also how to convert between them. This includes constants, which can—for the most part—be considered as variables that may be set only once.

You needn't understand some of the more complicated parts of this chapter, such as references or variable variables, unless you want full comprehension of the language. Most people will gain this knowledge through time and experience.

Types of Data

PHP has seven data types , and all but one hold a specific kind of information. The seven types are: string, integer, float, boolean, array, object, and resource. You'll be using them all at different times throughout this book, so it is worth remembering what they are.

Strings hold characters (literally: a string of characters) such as "a," "abc," "Jack and Jill went up the hill to fetch a pail of water," etc. Strings can be as short or as long as you want—there's no limit to size. PHP considers strings to be case-sensitive (i.e., Foo and FOO are different), which means that some string functions have case-insensitive equivalents.

Integers hold whole numbers, either positive or negative, such as 1, -20, 55028932, etc. There is a maximum limit to the size of integers—any numbers lower than -2147483647 and any numbers higher than 2147483647 are automatically converted to floats, which can hold a much larger range of values.

Floats hold fractional numbers as well as ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.