Using a variable in a PHP program

A variable is a holder for information in a computer's memory. Variables are labeled with a name, such as "&username = "Barbara."" When storing a variable, HTML and PHP is part of the source code. Similar to the previous two programs, the PHP code uses the <? and ?> special tag. The variable always begins with a ($) dollar sign to signify that it is a variable, a descriptive name, an (=) equal sign in PHP represents the assignment and "Barbara" is the variable value.

Tips to remember when naming your variables:

  • Always ensure that your variable name describes the information that it contains.
  • The length of your variable should be long enough to describe it accurately.
  • Do not use spaces in a variable name.
  • Do not use special characters and punctuation with the exception of ($) dollar sign and (-) underscore.
  • PHP is a case-sensitive language, so if you use $USERNAME and $username, you have two different variables.
  • Spelling your variable correctly is important, PHP will not catch a misspelling and will assume it is a new variable.

As we demonstrated in the previous programs, we used notepad a plain text editor and stored the program on our web server, "C:\Wampserver2\www."

The steps to writing a PHP program are outlined below:

1. Select and click "Start."
2. Select and click "All Programs."
3. Select and click "Accessories."
4. Select and click "Notepad."
5. Type the HTML and PHP exactly as shown below.

<html>

<head>

<title>Hi Barbara</title>

</head>

<body><h1>Hi Barbara</h1>

<h3>Demonstrates how to use a variable<h3>

<?php

$userName = "Barbara";

print "Hi, $userName";

?>

</body>

</html

6. Once you have type the entire program, Select "File," "Save As," and then go to the webroot of your server. For example, C:\Wampserver2\www. Type Filename, "barbara.php," and then Select "Save."

7. Use your browser to view the tips.php program. The steps are outlined below:

  1. Select and click "Start. "
  2. Select and click "All Programs."
  3. Select and click "Internet Explorer."
  4. Locate the Web Address Field, near the top-left of the Internet Explorer window and type in the Web address of the barbara.php program, "http://localhost/Barbara.php," then enter. The barbara.php program will be displayed.
Advertisement

, Tampa Information Technology Examiner

Ginny Putscher, a 25-year information technology veteran, has worked with both private industry and federal government. Ginny's experiences include traveling and working in many foreign countries - Egypt, El Salvador, Kenya, Tanzania and Senegal. Ginny received her BA in Database Development...

Today's top buzz...