Tutorials PHP PHP Basics

PHP Basics

This is a site to teach people the power of PHP. This is my first tutorial and I plan on updating the site once a week, probably on weekends when I have a bit of spare time. The first few tutorials might feel like a lecture but it'll pick up once we get the basics down.

Before you start

In order to code PHP, there are a few requirements. You need internet, a computer and a server which supports PHP. Most paid website hosting servers include PHP functionality. If you do not have a PHP server, you can use MAMP for Mac or WAMP for Windows as a virtual server to process PHP.

Basic PHP information

PHP is not very friendly. Most browsers, when confronted with unknown HTML, CSS, or JavaScript code, will attempt to display to the best of its ability, but PHP will just stop and give you an error. In advanced projects, it only takes one spelling mistake in the top of your code to destroy everything down below. It should be relatively easy to fix because PHP should give you an error with the line saying where there's a problem, but it's when you don't know about the error and your users come and find it that creates a problem. That's why you should always try to test out any new piece of code you write. Although I should really follow my own advice, since I routinely forget to test out my own code, especially after I make small minor changes.... Now, enough of me babbling; on with the tutorial.

PHP tags

Like HTML, PHP needs it's own tags to operate. You can put these tags anywhere inside a document with a .php extension. Similar to HTML, and almost any other code, everything that opens has to close. Everything relating to PHP must be between these tags. You can open and close the tags multiple times in the same document.

This is the basic PHP opening and closing tags:

<?php ............ ?>

Useful PHP functions

What makes PHP so wonderful? It's the beauty of dynamic content. PHP interacts with the server to process data for you. That probably doesn't make much sense right now but you'll know what I'm talking about soon. Almost every function in PHP has to end with a semicolon (;).

The "IF" function

I'm giving you a loaded rifle on your first day of training here. The IF function is easily the most powerful feature PHP has to offer. It checks if something is true, hense the name. I know that sounds cheesy but once you start coding PHP, the IF statement will become your best friend. It's so powerful and flexible that I don't even know where to start explaining it, so I'll leave it for now and come back to it in later tutorials. But just keep in mind that PHP without the IF function would be like a car without gas; it's that important.

You can pile more IF statements with the ELSE IF command. You can also add an ELSE at the very end for when the answer is none of the above. If your score is one, you get first prize, if your score is two, you get second prize, if your score is three, you get third prize, if the score isn't any of these, go try again next year. You can add as many ELSE IF statements as you want or you don't even need to add any if you only need to check one thing.

This is an example of an IF statement:

if ($name == 'tommy') {
// Your name is Tommy
} else if ($name == 'george') {
// Your name is George
} else if ($name == 'simon') {
// Your name is Simon
} else {
// I don't know your name
}

PHP comments

It is a very good idea to comment all of your code. When you have a large document, it becomes hard to keep track of everything. Unlike HTML comments, PHP comments are hidden from your users and only people with access to the actual PHP file will see the comments. So it's possible, although not recommended, to put stuff in your comments you don't want your users to see.

There are two ways to comment in PHP:

// PHP will skip on everything on this line after two slashes

/*
A slash and an astrix
is a block comment,
which means PHP will
skip over everything
until you close the
comment.
*/

Variables

If anybody has taken any high school math course, they would know what varibles are. Letters, numbers, words that could mean anything. PHP allows you to create your own variables that can hold anything. It might seem useless now but variables will be very useful when you start coding. The great thing about variables is that you can maipulate them.

Variables start with a $ and is usually followed by a = to assign the value.

$x = 20;
$y = 10;
$z = $x - $y;

The "echo" function

The echo function is another very handy tool. It basically shoots out code to HTML and can process variables and strings. If you only have one little bit of HTML code that you don't want to close and open PHP tags again for, just use echo to pump it out. If you have a variable, you must process it through PHP first via echo.

<?php
$name = 'Jessica';
echo '<p>My name is'.$name.'.</p>';
?>

would pump out:

<p>My name is Jessica.</p>

I know this might seem boring or hard and there's a few more lectures like this, but you'll be thankful later that you have the groundwork done. You have to crawl before you learn to walk, and you have to walk before you learn to run. So try and stick with the program and once you understand how and why, everything becomes easier. Well that's pretty much it for this tutorial. Come back for my next tutorial in a week.

Posted by on . Category: PHP


Comments

No comments posted yet

You need to register or login to post new comments.