.
Tutorial
Avatar

PHP basic lesson 3

By Remco on 2008-11-20
Views: 1049

Introduction

You now know that you can output HTML code by using the echo command.
But why would you use PHP if you would just use HTML.. Eh ??

This tutorial will explain what is so usefull about using PHP in combination with HTML and such.

Step 1 - Using variables

The most useful thing of PHP and many other scripting languages is that you can use variables.

Information

Variable examples:
  • Strings - Text values
  • INT's - Single number values ( 1, 2, 3 etc )
  • DOUBLE's - number values, sepperated by a comma and the numbers behind it. ( 1,345436 )

And many more.
In many languages you will have to create a specific variable like this:
String newString = "mytext";

PHP does this different, PHP will automaticly see what variable type it is.
This is not the only difference between PHP and other languages, PHP variables always start with a dollar sign ($).

Example:
$string = "mytext";
$string will automaticly get detected as a string because it contains text.
If we would say:
$string = 1;
PHP will see $string is an INT.



Step 2 - Use variables inside your PHP.

I will now show you how you can use variables in your PHP code.
We will edit the same file as we used before to keep things simple.

Replace the code with the code given to you below here, I will explain what happens in here in a second.



Code:

<?php

$name 
"Diederik";
$age 3;
$count 5;
$newage $age $count;

echo 
"
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> 
<html> 
<head> 
    <title>Hello world!</title> 
</head> 
<body> 
Helloo my name is "
.$name."<br>
I am "
.$age." years old.<br>
In "
.$count." years i will be ".$newage."
</body> 

</html>"


?>



Step 3 - Explaining the code

As you can see in the code we have created 1 String and two INT's.
With the INT we were able to calculate his new age, a very simple procedure.

But what happens here?
Helloo my name is ".$name."<br>
A string variable is getting pasted into a string.

It's a very simple procedure which is recommended to use when you want to use any variables inside your string.
The double quote " ends the text in the current string, the dot tells PHP to paste the variable in the string.
The dot means that you connect a string to a string.

explainstring.JPG



You have now learned how to use variables inside a PHP script.
« Previous Next »




Comments

Flamer - 2008-11-20
Hi Remco! Maybe it would be usefull to explain why you have to use the Dots when echo'en strings and PHP variables.
echo "Hallo my name is ".$name." and im almost 20 years old";

The dots are to connect strings with variables. Otherwise PHP will not recognize the Variable and will display "$name" instead of "Diederik".

Remco - 2008-11-20
I know I know Hehe .. I'm currently not at home.. i have to edit my function.. cos there is some stuff i can't do just now.
I'm still finishing this tutorial, working on it now.. then fixing it when i get home.. which will be in eeehm 1 hour 20 minutes ish lol Happy.

You have to be logged in to write a comment.

.