PHP variables
Last update on: 09-23-2008Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script. We will now return to the point, know and use different variables.
Definition:
There are several types of variables in php, the ones that you define yourself by giving a value, environment variables, the variables of sessions etc...We'll cover in this course, variables that you define and variables of environment, the variables sessions will be explained in a specific lesson.
Define a variable:
You must keep in mind throughout this course, how name a variable and give them a value, look at the following example.$variable = 'hello'; //we define a variable
print $variable; //we display its value
how it works?
A variable works in the form variable_name = variable_value, remember well the operation, this is part of the structure of a programming language. When you give a value to a variable name, to see this one at the screen, call the name of this variable and its value will be displayed.display variables and text on the screen:
You can display sentences, variables with several ways in PHP.Example 1:
$name = 'visitor'; //we give a value to the variable name
echo "hello $name"; // we display the result with echo and the "
Example 2:
$name = 'visitor'; //we give a value to the variable name
echo 'hello'.$name; //we display the result with echo and the '
Caution: Note that there is a . between the ' and the variable $name, the point is a concatenation variable to the string. Never forget this or you will get an error message in your page.
Example 3:
$name = 'visitor'; //we give a value to the variable name
echo 'hello $name'; //we display the result with echo and the '
Note: In fact, when you use the 'instead of the ", the variable is not interpreted as a variable but as string.
Attention: When you use ' and if in your text you have apostrophes, you must precede it by a backslash, for example:
the sentence
echo 'it's beautiful'; will display an error
you have to write like this
echo 'it\'s beautiful';
Also for the quotes the phrase:
echo "<a href="http://iteachweb.net">Learn php</a>"; will display an error.
You have to write like this:
echo "<a href=\"http://iteachweb.net\">Learn php</a>";
the sentence
echo 'it's beautiful'; will display an error
you have to write like this
echo 'it\'s beautiful';
Also for the quotes the phrase:
echo "<a href="http://iteachweb.net">Learn php</a>"; will display an error.
You have to write like this:
echo "<a href=\"http://iteachweb.net\">Learn php</a>";
To display variables or text you can use print too instead of echo it's up to you. maybe we will talk about the deferences between them later.
Now to your keyboard and start typing this examples and then move on to the next lesson (the variables of environment).
PHP and MySQL's lessons:
Introduction To PHPGet Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

