What is a PHP Variable
PHP variable is a
container or box that is used to store data, a variable name should be a
descriptive name like firstName, phoneNumber, emailAddress, author etc. Avoid
using short names example, a for age or h for height. Apart from you no one else
knows what you mean by a and h and to make someone understand what those short names stand
for you start adding comments to your code. In PHP variable names begins with a
dollar sign ($).
Example 1: Storing name, age and phone number of a customer within a variable then render the result on web browser.
<?php
$name="Joseph Onyeke Ameh";
$age=31;
$phoneNumber="08131294018"
echo $name;
echo #age;
echo $phoneNumber;
Output
Example 2: Writing a
short story including name and age of the character then replace the name and
age with a variable.
<?php
echo "There was a man called Musty, Musty is 70 years old, Musty loves being Musty but hate being 70 because 70 comes with gray hair";
$name="Joseph O Ameh";
$age="31";
echo "<br>";
echo "There was a man called $name, $name is $age years old, $name loves being $name but hate being $age because $age comes with gray hair";
?>Example 3: Write a PHP script that will Sum two values and outputting the result
Difference Between Single Quote and Double Quote in PHP
With a double quote you
can embed variables into your string while that can not be done using single
quote. This simple means if you write your variable name within your a double quote, it will display the value of the variable as an output but if we use a single quote it will display the variable as it is writing within the code (the variable name is converted to a text within a single quote).
Example: Script showing how to make use of single and double quote.
Concatenating String in PHP
Concatenation is the act of joining two or more strings together. Most programming languages uses the + (Plus) sign to concatenate string but PHP uses . (Period) to perform same task. In most cases the strings are stored within a variable so programmers end up joining the variables together.
Example