O'Reilly logo

Learning PHP and MySQL by Jon A. Phillips, Michele E. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coding Building Blocks

To write programs in PHP that do something useful, you'll need to understand blocks of reusable code called functions or methods, and then how to temporarily store information that cannot be executed in variables. We talk about evaluations, which are basically things that allow your code to make intelligent decisions based on mathematical principles and user input.

Variables

Since you haven't done any programming, we understand that variables are a new concept. A variable stores a value, such as the text string "Hello World!" or the integer value 1. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again for the entire life of the variable, which can be frustrating and tedious. Figure 3-2 shows a newly created variable that has been assigned a value of 30.

A variable holding a value

Figure 3-2. A variable holding a value

In PHP, you define a variable with the following form:

$variable_name = value;

Pay very close attention to some key elements in the form of variables. The dollar sign ($) must always fill the first space of your variable. The first character after the dollar sign must be a letter or underscore. It can't under any circumstances be a number; otherwise, your code will not execute, so watch those typos!

  • PHP variables may only be composed of alphanumeric characters and underscores; for example, a-z, A-Z, 0-9, and _.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required