PDA

View Full Version : PHP tutorial for total noobs :p (Part 2)



T0X!C-uk
30-04-2006, 01:28 PM
Second part of the Tutorial will be here and the next part will be in another post etc etc. as i cant seem to edit any of the posts i make.

Variables (Part 2.a)
If you have never used any coding language before then Variables may be very new to you.
Variables can be anything you like as long as you follow the rules below.
1) PHP Variables start is the dollar sign ($)
2) PHP Variables end with a semicolon (;)
3) PHP Variables must start with a letter or underscore.
4) You can only use alpha-numeric characters and underscores in Variables a-z, A-Z, 0-9, or _
5) If using more than one word in your Variable you should sperate the words with an underscore.

Variables are a way of storing information on a page untill your request to use them. Using Variables can save a lot of time by not needing to keep typing things over and over again. Notice below that when using numbers in variables you do not use speachmarks.

Variable Example:


<?php
$name = "T0X!C-uk";
$age = 18;
$lucky_number = 2;
?>

(Note: PHP does not require variables to be declared before being initialized.)

Echo's (Part 2.b)
As you may have saw in my other tutorial an "echo" is a way of outputting text on a webpage. As learn more about PHP and start coding you will notice you use the echo function more than any other.

Outputting a Variable
I am now going to show you how to output one of the variables we created above using the echo function.



<?php
$name = "T0X!C-uk";
$age = 18;
$lucky_number = 2;

echo "My name is $name<br />
I am $age years of age<br />
My lucky number is $lucky_number";
?>


The above code will output the following:
My name is T0X!C-uk
I am 18 years of age
My lucky number is 2

When echoing variables you can use HTML to format how the text is displayed.

Example:


<?php
$name = "T0X!C-uk";
$age = 18;
$lucky_number = 2;

echo "My name is <b>$name</b><br />
I am <b>$age</b> years of age<br />
My lucky number is <b>$lucky_number</b>";
?>


The above code will output the following:
My name is T0X!C-uk
I am 18 years of age
My lucky number is 2

As you can see it is very simple to edit PHP using HTML.

Go to Step 3 to learn an important part of PHP. We will be looking at Operators, How to include a PHP file on your main webpage and the IF Statements

Want to hide these adverts? Register an account for free!