Assigning Values to Variables

The equal sign (=) is used to set the value of a variable. This is called the assignment operator. On the left side of the assignment operator is a variable that will receive a value. On the right side is an expression, which could be a simple string constant or a complex combination of operators, variables, and constants.

When you assign a value to a variable, its type will change to fit the type of data you put into it. This is in contrast to C, which tries to convert values to fit the type of the variable. Assigning an integer to a variable that previously held a string converts the variable to an integer.

Table 2.2. Examples of Variables Assignments
String ConstantsInteger ConstantsDouble Constants
$myString = ...

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites 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.