Basic PHP Tutorial

imkingdavid

Reputable
Joined
Nov 26, 2009
Messages
161
Reaction score
0
FP$
885
Basic PHP Tutorial

Disclaimer
If you find any mistakes, let me know. If you have any questions, hit reply to this topic and I'll try and help you out! If you think I left something out and want me to add it in, let me know! Thanks!

Preface
This tutorial is aimed at the complete and total PHP newbie/beginner.

PHP: What is it?
According to Wikipedia, our (un)reliable (but still quite useful) friend:
PHP, or PHP: Hypertext Preprocessor, is a widely used, general-purpose scripting language that was originally designed for web development, to produce dynamic web pages.
As it said, it's a scripting language used for web development. More specifically, it is a server-side web programming language, used to create dynamic websites and web applications. "Server-side" just means it interacts with the server, as opposed to a client-side language, such as HTML, which interacts with the client/web browser.

Let's get started, shall we?
We shall. All PHP files must have the file extension ".php". Otherwise, the server will not be able to properly parse the code and will generate errors all over the place, or else it will simply display the code for all to see. There are ways of getting a file in another format to parse PHP code, but we won't get into that here. For now, just make sure all of your filenames end with .php and you'll be fine.

So, let's create our first PHP program! Don't worry, it's painless and easy.
To begin, open a new document in your favorite text editor. You can even use Window's native Notepad if you don't have anything else, but there are some nice alternatives, such as Notepad (FREE) and Dreamweaver CS4 (NOT FREE; 30-day free trial), and they do help greatly with syntax highlighting and other useful features, but they aren't required. But I digress.
Open a new document (normally, just click File -> New; in a more advanced program, you must specify the type. In this case, choose "PHP").
Remove any pre-inserted text from the file, as it is not needed right now.

You will notice that in some of the more advanced editors, there are line numbers. These are not to be confused with older programming languages that relied heavily on line numbers; these have no syntaxial value whatsoever, and are included for reference.
On line one, type the following:
PHP:
<span class="syntaxdefault"></span><span class="syntaxkeyword"><?</span><span class="syntaxdefault">php</span>
That will tell the server that it is time to start parsing everything from there until the closing tag for PHP. Anything in between that does not follow the PHP syntax will cause a syntax error and the program will not be able to run.
On the next line, type the following:
PHP:
<span class="syntaxdefault"></span><span class="syntaxcomment">//I am a single line comment.<br />#I am another single line comment<br />/* I am an inline/multiple-line<br />comment. See how I'm on two lines?*/</span><span class="syntaxdefault"> </span>
Those are comments, as you may have already guessed. Anything that is a comment is completely and totally ignored by the server/parser. Comments are used generally for two purposes:
  1. Documenting - Because most applications coded with PHP are open source, anyone can view the code that you use if you release it to them (although no one can if they do not have the .php source files). To make all of our lives easier (including your own down the road, unless you have a perfect memory), it is a good idea to use comments to tell what some of the specific lines and blocks of code do. While it is not required, it is a good idea if you plan on releasing your code for others to look at or help debug. Which brings me to...
  2. Debugging - Because nothing that has been commented out is seen by the server, it is helpful to use comments when you are getting an error when trying to run your application but cannot find exactly what is causing the error. Comments help you to isolate entire sections of code. We will get into this at another time, but in all basics, comment the entire file and then start removing the comments line by line until you find the line that is generating the error. Yes, it's time consuming, but you'll waste even more time just sitting and staring at a screen with no idea what's wrong.
Finally, type the following on a new line after your comments.
Code:
?>
This tells the server to stop parsing code as PHP and move back to HTML. This is useful for including HTML and PHP in the same file (NOTE: HTML can be put in a .php file, but PHP cannot be put into a .html file).

echo()
Ok, now empty out the contents of that file you just created and type the following into it.
PHP:
<span class="syntaxdefault"><?php<br /></span><span class="syntaxcomment">// This file will output "Hello World" onto the client's screen.<br /></span><span class="syntaxdefault">echo </span><span class="syntaxstring">'Hello World'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">?></span>
Now save your file and upload it to your web server, and run it in your browser. You should see "Hello World" (WITHOUT the ") in the top left corner of your screen. If so, congratulations; you just ran your first (or one of your first) PHP file!

An important thing to note is the semicolon ; at the end of the third line. This tells the server that it has reached the end of a line of code and should move on to the next line. These must be included at the end of each command. However, there are times when you should not include it. But we'll get into that when the time comes. If this seems confusing, it will make more sense later.

Now I will explain what echo is. PHP has a whole slew of native "methods" (aka functions) that allow you to communicate with the server. These range from methods that allow you to print text on the screen to ways to store data in a database and alter files, among other things. echo is one of them.

Basically, echo is a method that outputs all characters between the quotation marks as HTML code (as if you were not in between <?php and ?> tags). This way, you don't have to keep opening and closing the PHP tags in order to display something on the screen.
So if you were to type
PHP:
<span class="syntaxdefault"><?php<br />echo </span><span class="syntaxstring">'<strong>Test</strong><em>ing</em>'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">?></span>
Then you would see on your page

Notice that in my example, I used apostrophes, ' (from now on called single quotes), to wrap around the string of text. You can also use quotation marks, " (from now on called double quotes), but there is a difference.
  • Use single quotes when you are only using simple text/html, or don't mind typing slightly more in order to use variables. Single quotes are parsed more quickly by the server because the parser does not have to search between the quotes for variables to parse (we'll get into variables later).
  • Use double quotes when you are trying to use variables inside the quotes. Double quotes are parsed more slowly because the server has to check each character between the quotes to make sure it doesn't need to be parsed.
**In most cases, it is best to use single quotes unless you really need to use double quotes. Even though double quotes only slightly increase the page load time, if you use them a lot, it makes a bigger difference.

So now you know some of the basics of PHP. In review, we learned...
  • All PHP files must have the .php extension so that the server knows to parse the code as PHP.
  • Wrap all PHP code between <?php and ?>.
  • There are three types of comments, two that make everything after them on the same line into a comment (// and #), and one that comments out everything between the opening and closing tag (/* and */).
  • To output text to a page, use PHP's native method: echo.

This tutorial is Copyright(c) 2009 - David King/imkingdavid; originally posted on phpBBDevelopers.net. Do not repost without written approval. If you link to it, you must give credit.
 
Good tutorial, a little thing though, you dont always have to use <?php ?> , you can use shorthand, which is <? ?> on some servers.
 
ThePimpedMistletoe said:
Good tutorial, a little thing though, you dont always have to use <?php ?> , you can use shorthand, which is <? ?> on some servers.
True, although if you're developing software that will be distributed, using the <? and ?> is sometimes not supported on some servers. So while it does work in most cases, you should use the full version <?php to be safe, unless you are doing something for only your own website and you know it works. 🙂

Thanks for the input. 😀
 
imkingdavid said:
Code:
//I am a single line comment.
#I am another single line comment
/* I am an inline/multiple-line
comment. See how I'm on two lines?*

Just a little bit of nitpickery here: you missed the closing / on your inline comment. Should be /* */ but is /* *.

Will there be any more php tutorials? Perhaps some that will teach those of us who know a little php something?
 
nice tutorial! this helped me start out on learning php. you should make a whole series of these 😀
 
Wolvendeer said:
imkingdavid said:
Code:
//I am a single line comment.
#I am another single line comment
/* I am an inline/multiple-line
comment. See how I'm on two lines?*

Just a little bit of nitpickery here: you missed the closing / on your inline comment. Should be /* */ but is /* *.

Will there be any more php tutorials? Perhaps some that will teach those of us who know a little php something?
Oops! Good catch. I fixed it.

As far as more tutorials, I could write a few more if people want them. 🙂

Thanks for the feedback! 🙂
 
Really nice and useful tutorial. I am also doing PHP programing and this tutorial will be proven quite helpful. Using this tutorial make easy for php programming. PHP is very easy to learn. It is a acceptable caution as the first step in the programming world.
 
Back
Top Bottom