imkingdavid
Reputable
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:
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.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.
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>
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>
- 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...
- 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.
Code:
?>
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>
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>
Testing
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.
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.







