Sign up for Semrush FREE Trial

Saturday 15 June 2013

PHP Coding for Beginners

What is PHP?
PHP is what's known as a server side scripting language. To put it simply, all php code is processed on a server and anything that is outputted by the code is sent to the browser as html. It allows website users to gain controlled access to server based resources, e.g. database and files based on the server.

Starting Your PHP Code
All php code is wrapped like so... <?php PHP CODE GOES HERE?>
Anything within these tags will be processed by the server as html.



Variables
A variable is a way of storing information. For example you can set a variables value to anything you wish. e.g.
$variable1 = "hello";
Here we set a variable named "variable1" equal to "hello". Notice the dollar sign, this tells the server that we are setting a variable. Also the line is ended with a semi-colon, this tells the server that the variable is finished being set. Semi-colons are to be used to separate code and allow the server to read and process the code correctly.
Variables can also be joined e.g.
$variable2 = $variable1.$variable3;

Comparing Variables
Variables can be compared using an "if" statement as follows:
if($variable1 == $variable2) {
$variable4 = "yes";
} else {
$variable4 = "no";
}
The if statement works by comparing the values of variable 1 and 2. If they are equal then variable 4 will be set to yes, if they are not then variable 4 will be set to no.
The basics of the if statement works in the following way:
If anything within the brackets returns true then run the code within the squiggly brackets "{}". If an else statement is present (not essential) then if whatever was in the brackets returned false the code within the second set of squiggly brackets will be run.
Note the use of the double equals, this is to tell the server we are comparing two values and not setting the value of a variable.

Outputting Simple Data
Up to now we have not written any code which would output any html.
Here we learn about the echo function. This functions prints data out to the page as follows:
echo("hello"); //returns hello
echo($variable1); //returns the value of variable1
Note the use of the semi colon again to terminate the line.
Now you've learnt the real basics
You'll be surprised how much this simple tutorial can help you. Although these are the real basics they are used in every single php page I have programmed in my 8 years of programming.
Try and create a php file to output data, depending on the value of a few variables using what you've learnt about variables, the if statement and echo function.
Check back next week for another lesson.

Article Source: http://EzineArticles.com/7289987

1 comment:

  1. that is really a great blog very awesome information check this one php web developer
    thanks for sharing..

    ReplyDelete

I appreciate your valuable comment...