In this article we will begin with the basics and most simple aspects of PHP. We will learn how we print a specific text and how we define variables. These are the most simple, yet the most essential functions that everybody must learn, before continuing to the development of content-rich and functionality-rich web applications.
Like every programming language, PHP uses variables to define the values of items. A variable can contain a numerical or alphanumerical values or symbols. Variables in PHP have always a dollar symbol prefix. This sounds familiar to people who already have some knowledge of Perl.
Here is an example where we define the value of a variable and later we print it on the screen using the print() function:
CODE
<?php
$text = "Hello world!";
print $text;
?>
$text = "Hello world!";
print $text;
?>
which will simply output:
Quote -
Hello world!
Another example:
CODE
<?php
$greeting = "Hello everyone!";
$text = "$greeting How are you?";
print "$greeting $text";
?>
$greeting = "Hello everyone!";
$text = "$greeting How are you?";
print "$greeting $text";
?>
The output result will be:
Quote -
Hello everyone! How are you?
When a variable is followed by the "=" symbol it means that we define its value for the first time or we redefine it and overwrite any previous value if exists. When it is followed by a ".=" symbol (notice the dot before the equal symbol), it means that the second value is appended to the first one.
Here is an example which explains this better:
CODE
<?php
$text = "Good morning. ";
$text = "How are you?";
// Notice that in the second line we used the = symbol
// without a dot
print $text;
?>
$text = "Good morning. ";
$text = "How are you?";
// Notice that in the second line we used the = symbol
// without a dot
print $text;
?>
The result will be the value that was last defined:
Quote -
How are you?
Now we will see how the .= symbol works:
CODE
<?php
$text = "Good morning. ";
$text .= "How are you?";
// Now we used the .= symbol
// with a dot
print $text;
?>
$text = "Good morning. ";
$text .= "How are you?";
// Now we used the .= symbol
// with a dot
print $text;
?>
Quote -
Good morning. How are you?
Important
When we print a variable using the echo or print command, it is essential that we use the double quotation marks or no quotation marks at all. Single quotation marks will not print the value of the variable, but the name of the variable itself.
An example again, will give more light to this:
CODE
<?php
$text = "Hi there";
print "$text";
?>
$text = "Hi there";
print "$text";
?>
Result:
Quote -
Hi there!
which is what we wanted to show.
But if we use single quotation marks:
CODE
<?php
$text = "Hi there";
print '$text';
?>
$text = "Hi there";
print '$text';
?>
It will print:
Quote -
$text
In the second part of our PHP articles we will learn specifically about the usage of symbols within variables and when we need to use escape characters to avoid parse errors. We will also learn how we use data passed in a GET query (like file.php?id=1&item=this ) or POST query.
Next parts will be published here on Neowin and also on my blog: Learn PHP Online