Vincent Reniers | From PHP Guru to Designer!

TAG | Syntax

Mar/09

9

PHP : Basics Explained

1.Introduction
What is PHP? PHP is a server-side programming language. This means it processes a certain script to create a dynamic output in HTML. In most cases PHP is used together with some sort of Database such as MySQL. For example when a user clicks on the news page. The News.php script can ask the 10 latest news articles from the MySQL database. And while it is requesting this information it can sort it out in HTML tables with a heading, comment button etc. When you’re just using HTML you can’t do this and your information will always be static. Now let us take a quick view at how a PHP script may be.

<?php
echo("Hello Visitor");
?>

This will just output the string : Hello Visitor. As you can see the PHP script starts with <?php and ends with ?>. This tells the server when the PHP module will have to start processing the script and when the normal static HTML begins ( after the ?>). Echo is the function. Functions can handle information the way you want them to deal with it. But we’ll come to that later. As you can see we tell the script that we’re processing a string by placing it between the ” ” quotes. And each function ends with a ; character. Let us continue with variables.

2.Variables

<?php
$variablename  = "content";
echo $variablename;
?>

This script will output : content . Variables are used to store information. Just temporary of course. After the script is done at the very very end. When the client is looking at the page with all the processed information. At this part the PHP script will automaticly remove these variables. This is an automatic process wich is called the ‘Garbage-collector’. But you can delete variables yourself if you like. You can do it this way.

<?php
unset($variable);
?>

3.Cookies
This is mostly advised when the variable holds a large amount of data. So you can unset it when you no longer need it. But when using normal scripts you don’t need to worry about it. Ok so how can you store information permantently? Through databases, with sessions or with cookies! Yes cookies =D. Cookies are stored in your browser, but of course the visitor needs to allow the cookies to be stored in his settings. Thats why you can’t login on sites when you don’t store cookies. For example when you login on some site. And your pass and username are correct the script normally creates a cookie with the name ‘username’ and as value your name. And a cookie ‘password’ and your password. And each time you go to a page the script checks if you have a cookie username so it recognises you as logged in. You can set cookies like this.

<?php
setcookie("username", "vincent"); // for example
?>

4.IF and ELSE statements
This is how you set a cookie. The text after the // is seen as a comment and the PHP script simply ignores this. Now you know what functions can do. PHP has a lot of functions preprogrammed wich you can find on www.php.net As for the basics of PHP i’m going to explain you another aspect. After this you will understand the very basics and you will be set to learn a lot more in the documentation or with other tutorials.
The IF and ELSE statements. These are so handy. Like suppose you want to see if a user is logged in.

<?php
$user = $_COOKIE['username'];
if($user){
echo("Welcome $user");
}
else{
echo("Welcome Guest");
}
?>

The user variables gets the information from the cookie ‘username’. If it exists and if there is information in the cookie. It will welcome the user and display its username. Otherwise it will welcome the guest.
The actions that will occur when the statement is true are always located between the { } brackets.

So that was my tutorial about the basics of PHP explained. I hope you have learnt a lot. And if you have any more questions don’t hesitate to comment below and ask your question =).

, , , , , , , Hide

Find it!

Theme Design by devolux.org