Hi , today i will show you how to write variables in PHP and it's will be in tow parts this part content 5 topics :
- Naming variables
- Assigning values to variables
- Using constants
- Removing variables
- Handling errors
OK , what is the variables ?
it's variables are containers that hold information. First, you give a variable a
name, and then you can store information in it .
For example, you couldname, and then you can store information in it .
name a variable $age and store the number 21 in it. After you store information
in a variable, you can use that variable later in the script .
Naming variables
When you naming a variables you should to put a clear name for it , like $fristName or $email .
but don't use variables name like $var1 , $var2 , $var3 etc... , because it's not useful to understand the code .
The rules for variables naming :-
- All variables start with dollar sign ( $ ) .
- Variables can be any length .
- Variables names can includes letters, numbers, and underscores only.
- Variable names must begin with a letter or an underscore .
- Uppercase and lowercase letters are not the same
The following are valid variable names:
$_name
$first_name
$name3
$name_3
The following variable names cause error messages:
$3name
$name?
$first+name
$first.name
The first name is invalid because it doesn’t begin with a letter or an underscore,as required. The three remaining names are invalid because they containcharacters other than numbers, letters, and underscores.
Assigning values to variables
to store data in variables you can do that by following the variable name with single equal sign ( = ) , like this examples :-
$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;
You can store the value of one variable in another variable, as shown in the
following statements:-
$name1 = “Sally”;
$name2 = “Susan”;
$favorite_name = $name2;
You can create a variable without storing any information in it. For example,
the following statement creates a variable:
$city = "" ;
Tip :
you can use variable like this , $fNmae.$lNmae .
to see the results : make
$fNmae = "Ahmed";
$lName = "Adm";
echo $fName.$lName;
the result is : AhmedAdm .
and you can use it like this , $fName." ".$lName ;
echo $fName." ".$lName ;
also you can use it like this , "Mr . $fName $lName "
echo "Mr . $fName $lName ";
the result : Mr . Ahmed Adm
Using constants
Constants are similar to variables. Constants are given names, and values are
stored in them. However, constants are constant; they can’t be changed by
the script. After you set the value for a constant, it stays the same .
Constants are set by using the define statement. The general format is as
follows:
define(“constantname”,”constantvalue”);
For example, to set a constant with the weather, use the following statement:
define(“WEATHER”,”Sunny”);
Removing variables
Simply you can remove the data from variables by using this built in function ( unset ) , you can use it like that :-
unset( $variableName ) ;
after that you can't use it and if you use it , you will see error message ! .
Handling errors
PHP tries to be helpful when problems arise by providing error messages. It
provides the following types of messages:
Error message : You receive this message when the script has a problem
that prevents it from running. The script displays an error message and
stops running. The message contains as much information as possible to
help you identify the problem. The following is a common error message:
Parse error: parse error in c:\test.php on line 6
Often, you receive this error message because you’ve forgotten a semicolon,
a parenthesis, or a curly brace.
Warning message : You receive a warning message when the script sees
a problem but the problem does not prevent the script from running.
Warning messages do not mean the script can’t run; they indicate that
PHP believes something is probably wrong. You should identify the
source of the warning and then decide whether it needs to be fixed. It
usually does. For example, you see the following message if you don’t
include a variable name in the print_r statement — print_r() rather
than print_r($varname).
Warning: print_r() expects at least 1 parameter, 0 given
in d: test1.php on line 9
Because this is a warning, not an error, the script continues to execute
the statements after the print_r statement. However, a warning usually
indicates a more serious problem than a notice. In this case, you need to
fix the problem.
Notice : You receive a notice when PHP sees a condition that may be an
error or may be perfectly okay. One common condition that produces a
notice is echoing variables that don’t exist. Here’s an example of what
you might see in that instance:
Notice: Undefined variable: age in testing.php on line 9
i hope you find that helpful for you , and if you have any thing put it in comments .




