Κυριακή 2 Μαρτίου 2008

PHP variables

Over the last years PHP has become the programming language used for the development of millions of web sites. We see it everywhere: most of the sites we use everyday have files with a .php extension in their URL. Forums, blogs, video sharing sites, file sharing sites, photo albums... even some official company sites. Therefore, the knowledge of PHP is the key to the success for a professional web developer.

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:

<?php
$text = "Hello world!";
print $text;
?>

which will simply output:

Hello world!

Another example:

<?php
$greeting = "Hello everyone!";
$text = "$greeting How are you?";
print "$greeting $text";
?>


The output result will be:

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:


<?php

$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:

How are you?


Now we will see how the .= symbol works:

<?php
$text = "Good morning. ";
$text .= "How are you?";

// Now we used the .= symbol
// with a dot

print $text;
?>


The result will be:

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:

<?php
$text = "Hi there";
print "$text";
?>

Result:

Hi there!

which is what we wanted to show.

But if we use single quotation marks:

<?php
$text = "Hi there";
print '$text';
?>

It will print:

$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.

The next article on this subject will be available approximately within the next week.

If you want to learn PHP and become a professional Web developer... within 1-2 days, I suggest that you use the "Learn PHP in 17 hours" series of books. This is the method I have also used. After reading these books I developed 18 PHP web sites within 3 months for my customers and they paid me more than 5.000 euros for these. Now I have quit my job and I am working only on Web development only 2-3 hours per day from my home. Actually, it was not my idea. A friend of mine suggested that I learn PHP and that I will be making a lot of money... and first I said "oh, yes, ok... another Internet millionaire on the scene... with a programming language that is available for free...". But he insisted on this. He was talking about it every day. So I decided to give it a try. Now I have seen with my own eyes that this is the revolution and it is the key to success on Internet business.

Links:
See more info about the "Learn PHP in 17 hours" books.

For more info you can visit often this blog: Learn PHP online.