Quantcast
Channel: Visual Magick » learn
Viewing all articles
Browse latest Browse all 5

How to PHP Variables Introduction

0
0

Variables are containers that hold information. First, you give a variable a name, and then you can store information in it. For example, you could
name a variable $names and store “Charlie” in it. When using PHP on the Web, variables are often used to store the information that users type into an HTML form, such as their names. You can then use the variable later in the script, perhaps to personalize a Web page by displaying the user’s name, as in, for example, Welcome Charlie. In this lesson, you find out how to create variables, name them, and store information in them.

The following Values are valid PHP Variables:

[codesyntax lang="php"]
$_name
$first_name
$name3
$name_3
[/codesyntax]

The following variable names cause error messages in PHP:
[codesyntax lang="php"]
$3name
$name?
$first+name
$first.name
[/codesyntax]

The rules for naming variable names are as follows:

All variable names start with a dollar sign ($). This tells PHP that it is a variable name.

Variable names can be any length.

Variable names can include letters, numbers, and underscores only.

Variable names must begin with a letter or an underscore. They cannot begin with a number.

Uppercase and lowercase letters are not the same. $somenamecity and $Somenamecity are not the same variable. If you store information
in $Somenamecity, you can’t retrieve that information later in the script by using the variable name $somenamecity.
Assigning and Displaying Variable Values:

Variables can hold either numbers or strings of characters. A variable can exist or not exist and can hold information or not hold information; these are
two separate ideas. Even if a variable doesn’t currently contain any information, it still can exist, just as a drawer exists even when it is empty. Of course,
if a variable contains information, it has to exist.

Creating Variables:

Storing information in a variable creates it. To store information in a variable, you use a single equal sign (=). For example, the following four PHP statements assign information to variables:

[codesyntax lang="php"]

$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;
[/codesyntax]

In these examples, notice that the numbers are not enclosed in quotes, but the name, which is a string of characters, is. The quotes tell PHP that the
characters are a string, handled by PHP as a unit. Without the quotes, PHP doesn’t know the characters are a string and won’t handle them correctly.

You can create a variable without storing any information in it. For example, the following statement creates a variable called an empty string, The variable now exists but does not contain any value.

[codesyntax lang="php"]
$city = “”;
[/codesyntax]


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images