Sei sulla pagina 1di 3

A variable is simply a container thats used to store both numeric and nonnumeric information.

And just as with any container, you can move it from


place to place, add stuff to it, or empty it out on the floor in a pile and fill it
with something completely different.
<h2>Naming Variables In PHP</h2>
<hr>
PHP has some simple rules for naming variables. Every variable name must
be preceded with a dollar ($) symbol and must begin with a letter or
underscore character, optionally followed by more letters, numbers, or
underscore characters. punctuation characters, such as commas, quotation
marks, or periods, are not permitted in variable names; neither are spaces.
<b>Example of some valid variable names are:</b>
<pre lang="PHP">
$name;
$age;
</pre>
<h2>Assigning Values to Variables</h2>
<hr>
Assigning a value to a variable in PHP is quite easy: use the equality (=)
symbol, which also happens to be PHPs assignment operator. This assigns
the value on the right side of the equation to the variable on the left. To use a
variable in a script, simply call it by name in an expression and PHP will
replace it with its value when the script is executed.
<b>Examples:</b>
<pre lang="PHP">
<html>
<head>
<title>Using Variables in PHP</title>
</head>
<body>
<h2>Demonstrates using a variable</h2>

<?php
$name='Rahul Upmanyu';
$country='INDIA';
$age=21;
echo "I am {$name} and I live in {$country} and I am {$age}";
?>
</body>
</html>
</pre>
<h2>Output of above program"</h2>

In above example, the variable $name is assigned the value 'Rahul


Upmanyu'. The echo statement is then used to print the values of variables to
the Web page.
<h2>Destroying Variables</h2>
<hr>
To destroy a variable, pass the variable to a PHP function named
<b>'unset()'</b> .
<b>Example of destroying a variable:</b>
<pre lang="PHP">
<html>
<head>

<title>Destroying Variables</title>
</head>
<body>
<?php
$name='Anjali'; // Asign Value to variable
echo "Name Before Destroying variable: $name"; // Print variable
value
unset($name);
echo "Name after Destroying variable: $name"; //this will
generate an 'undefined variable' error
?>
</body>
</html>
</pre>

<p style="background-color:#ffffff;"> <b>Note:</b> Trying to access a


variable that has been unset() in your preceding script, results in a PHP
undefined variable error message. </p>

Potrebbero piacerti anche