Share/Bookmark

Featured Posts

SQL AD Group membership Hi all, I have been working on a way to return Active directory group membership from a linked AD server in SQL. Its quite hard as you can`t return it from the group - you have to return it from...

Read more

Face Detection with PHP A very useful piece of PHP code that will highlight any pictures where a face has been detected. Heres the first piece of PHP code you will need, it would be best to make this an include: [sourcecode...

Read more

Basic Guide to Database Normalisation: First Normal... Before I start talking about the normal forms, I thought it would be best to explain what I am planning to write about, and what normalisation is. I am going to cover what normalisation is for beginners...

Read more

Sony: Synonymous with Innovation Sony have always been on the leading edge of innovation. From creating the WalkMan back in the 80's to popularising the CD, DVD and Blu-Ray with their PlayStation products. In this article we're discussing...

Read more

3 Useful Multimedia API's for Web Developers - Q1 2010 There are many useful API's out there for web development these days, in this article we review the top three you should certainly check out for displaying multimedia on websites you are developing. Screen...

Read more

PHP Variables and Echo

Posted by matthew | Posted in Tutorial | Posted on 26-11-2009

Tags:

0

PHP is highly scalable and will most likely be installed on a Linux of Unix web server by default, so below we introduce the basics of using this language. This is by no means definitive, for full documentation you should visit the official PHP website, but this article should help you understand the basics.

If you have PHP installed then simply create a page with the php extension and save it to your web server. Whenever you want to add PHP to a page you must enter into PHP mode. To do this you use an open tag like so:

<?php

and the you must close it at some point like so:

?>

You must also end every line in PHP with a semicolon (;).

Anything that is not inside a PHP container will be treated as HTML or what ever you have set the page to be at the very top, must likely you would use a standard page encoding like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="US-EN" xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="US-EN" xmlns="http://www.w3.org/1999/xhtml">

This encoding tag will treat the page as XHTML in transitional mode with the language of English, if none of that made sense you should really brush up on HTML and XHTML before reading more.

An important thing to remember is that PHP is run on the server, not by the client. This means you web server interprets the PHP code first and displays the final outcome to client, this will become more apparent further in this article.

Variables

In PHP you define a variable with the $ (dollar) sign, a variable is a piece of text or a number or both (a string) that you can store and use later within a page:

$var = 'cat';

This will define a variable called ‘var’ and set this to ‘cat’. If you want to add something to this there are a number of ways you could do this.

$var = 'cat and dog';

$var = $var.' and dog';

$var .= ' and dog';

The first example resets the variable to ‘cat and dog’, the second example reuses the varibale and adds ‘ and dog’ this is done by using ‘.’ (dot) which is the join operator in PHP. Finally the third example performs the same operation as the second but instead appends ‘ and dog’ to whatever the variable currently is using ‘.’ with the equals sign instead, this is useful if you plan to keep adding to a variable for example:

$var = 'a';

$var .= 'b';

$var .= 'c';

$var .= 'd';

The final results of the variable var will be ‘abcd’ but variables don’t have to be just text, take this for example:

$var = 4+6;

PHP will perform the calculation as ‘4+6′ as it not in quotes, so it will not treat it as a string. So variable var will actually be set to ‘10′. Not only can you perform calculations with numbers, but also other variables:

$x = '2';

$y = '4';

$i = $x*$y;

In this example a variable called ‘x’ is set to ‘2′, variable ‘y’ is set to ‘4′ and then variable ‘i’ is the result of ‘x’ multiplied by ‘y’ resulting in of course ‘8′.

Echo

The echo function prints either strings or variables to the screen, for example:

echo 'hello world';

This would print ‘hello world’ to the screen just like any other piece of text.

$var = 'hello'

echo $var;

This would print the contents of the ‘var’ variable to the screen so the result would be ‘hello’. Again we could use the join operator (.) for example:

$var1 = 'hello ';

$var2 = 'world';

echo $var1.$var2;

This would print both the contents of ‘var1′ and var2′ to the screen, you could also put a seperate echo on the next line down to achieve the same results.

However, if you plan to echo a mix of variables and static strings, you would need to do the following:

$bills = '400';

$money = '500';

echo "your bills come to ".$bills." however you only have ".$money." in the bank";

You will notice that every time we want to echo a variable we ‘come out’ of the string containers (quotes) and when we want to resume string echoing we encase the strings in quotes. You can choose to use either single quotes or double quotes providing you are consistent throughout.

That’s it for this post, but feel free to view our other posts where we talk about other PHP goodies such as arrays, loops and if statements.

Write a comment