Jump to content

[ARTICLE] PHP Part 1: Working with variables


Recommended Posts

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;
?>

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.

Next parts will be published here on MSFN.

You can also find more details on my blog: Learn PHP Online

Edited by ckgni
Link to comment
Share on other sites

  • 3 weeks later...

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...