Variables

A variable is a special container you can define to hold a value. Variables are fundamental to programming. Without them, you’d be forced to hard-code all the values in your scripts. In adding two numbers together and printing the result, we have achieved something useful:

print (2 + 4);

This script is useful only for people who want to know the sum of 2 and 4, however. To get around this, we could write a script for finding the sum of another set of numbers, say 3 and 5. This approach to programming is clearly absurd. Variables enable you to create templates for operations (adding two numbers, for example) without worrying about what values the variables contain. The variables are given values when the script is run, possibly through ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition 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.