1. Computing

Learn PHP - A Beginner's Guide to PHP Programing

By , About.com Guide

4 of 9

Variables
The next basic thing you need to learn how to do is to set a variable. A variable is something that represents another value.

 <?php 
 $like = "I like About";
 print $like;
 $num = 12345;
 print $num;
 ?> 

This sets our variable, $like, to our previous I like About statement. Notice again the quotation marks [“”] used, as well as the semicolon [;] to show the end of the statement. The second variable $num is an integer, and therefore does not use the quotation marks. The next line prints out the variable $like and $num respectively. You can print more than one variable on a line using a period [.], for example:

 <?php 
 $like = "I like About";
 $num = 12345;
 print $like . $num;
 print "<p>";
 print $like . " " . $num;
 print "<p>";
 print "My favorite number is $num";
 ?> 

This shows two examples of printing more than one thing. The first print line prints the $like and $num variables, with the period [.] to separate them. The third print line prints the $like variable, a blank space, and the $num variable, all separated with periods. The fifth line also demonstrates how a variable can be used within the quotation marks [""].

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.) Also note that if needed you can dynamically build variables. For a more thorough look at variables, see our Guide to Variables.

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP Functions
  5. Learn PHP - PHP Tutorial - Variables

©2013 About.com. All rights reserved.