PHP Tutorials: Creating Variables is Easy

Lesson 1: Using variables is easy.
For the first lesson in PHP, I am going to cover the simplest concept behind PHP, variables and the ability to echo variables. For this lesson we will learn the simple ways to create a variable and echo it to the user.

The Always Famous "Hello World"


Before we start, all you will need is a completely blank page in your text editor, dreamweaver, or any other html editor program. This means that you should delete all tags already included on the page such as <html><body><head> and everything else. You want a BLANK page.

It is easy to echo information from your page onto the browser. Before you begin coding, you need to have <?php at the beginning and ?> at the end of your practice page. These tags tell the server that there is code within the tags that needs pre-processing before it can be shown to the user. Since we will not be echoing this information within tables are standard html tags, the php tags are only needed once for this example.

Creating PHP Variables


The PHP coding, displayed below, gives you a simple idea on how to create variables and then echo them to the browser. Every variable begins with the dollar sign and can be named anything you would like; such as $name, $info, etc.

<?php
//firstlesson.php  no <html> tags whatsoever
//Part 1: the common Hello World

echo "Hello World";
echo 
"<br>"//for viewing purposes
//Part 2: Hello World Version 2

$variable "Hello World";
echo 
$variable;
echo 
"<br>"//for viewing purposes

//we just created a variable. A variable can have any name such as $name, $birthday
//you tell the variable what it is, or what it is equal to.

//Part 3: Your Birthday

$month "July";
$day "17th";
$year "1981";
//now we have setup every variable, now we can echo each variable:
echo "My Birthday Is ";
echo 
$month;
echo 
" "//create a space
echo $day;
echo 
", "//create a comma and space
echo $year;
echo 
"<br>"//for viewing purposes

//that will cause the page to say: My Birthday is July 17, 1981
//but there is an easier way to do this. Do an echo as one string:
echo "My Birthday Is "$month ." "$day .", "$year ."";
echo 
"<br>"//for viewing purposes

//the above option is the better way to do it, each variable must have quotation marks and a period around them
//notice that each variable is singled out by ". or .". I put spaces to make it easy to see the variables

//Part 4: Math Functions

$total = (10+2)*3//36
echo $total;
echo 
"<br>"//for viewing purposes

$anotherexample 30 8;
echo 
$anotherexample;
echo 
"<br>"//for viewing purposes
echo "Here is another example:"$anotherexample;
echo 
"<br>"//for viewing purposes
//using variables within the math
//using first example
$num1 10;
$num2 2;
$num3 3;

$newtotal = ($num1+$num2)*$num3//36
echo $newtotal;
echo 
"<br>"//for viewing purposes


//NOTE ON ECHO for Text and Variables
//variables on the end and beginning of lines to be echoed do NOT need quotations on both sides
//EXAMPLE
$text "My Birthday is";
echo 
$text ." "$month ." "$day .", "$year;

//the above string works fine too.  Unnecessary to have quotations at the beginning and the end. 

//END OF FIRST LESSON

//semi-colon is required
?>

See How the PHP file will look in browser window

This is a basic example of using/creating variables with PHP. The next lesson will cover actually puting the variables into html code.
VISITED
NA
NA
NA

FEATURED

EXTRAS
Make Us Your Home Page

Add Us to Favorites

Tell a Friend About this Page

Open Index to this Issue

SEARCH







SPONSORS