Share/Bookmark

Featured Posts

SharePoint Ticker Web Part from Existing Data Ever wanted to create a ticker in SharePoint without having to buy the $99 add-on from Microsoft, well here's the code: Simply create a new Web Part using the 'Content Editor' and open the 'Source Editor...'...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Whats next for Nintendo? Today I noticed that Nintendo have had falling profits for 2010 and predict even less sales for next year. The Wii 2 hasn`t been officially announced and from what I can see is not really on the radar....

Read more

7 Great Free Android Apps The Android mobile operating system is both fast, reliable and can do everything that the Apple OS can and a little more (all for normally a fraction of the price.) plus it supports Flash and soon Flash...

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