PHP




     

PHP Tutorial for Beginners

PHP is a powerful scripting language that fits gracefully into HTML and puts the tools for creating dynamic websites in the hands of the people — even people like me who were too lazy to learn Perl scripting and other complicated backend hoodoo.
This tutorial is for the person who understands HTML but doesn’t know much about PHP. One of PHP’s greatest attributes is that it’s a freely distributed open-source language, so there’s all kinds of excellent reference material about it out there, which means that once you understand the basics, it’s easy to find the materials that you need to push your skills.

Contents

  1. Introduction
    1. What Is PHP?
  2. What you’ll need
  3. Steps
    1. The Basics
      1. Basic Syntax
      2. Code Syntax
    2. Your First Script
    3. Error Messages
    4. A Few More Statements
    5. Very Able Variables
      1. Naming Variables
    6. HTML Forms and PHP
      1. Get versus Post
    7. Arrays
    8. Associative Arrays
    9. Operators; If, Else, Elseif; Loops
      1. If
      2. Else
      3. Elseif
    10. Comparison and Logical Operators
      1. Comparison Operators
      2. Logical Operators
      3. Aritmetic Operators
    11. Loops
      1. Other Loops
    12. Functions
    13. Some Final Thoughts
  4. Suggested readings

Introduction


What Is PHP?

So, what is this whole PHP business all about?
PHP is a program that gets installed on top of your web server software. It works with versions of Apache (Tutorial:Apache for Beginners), Microsoft IIS and other server software packages.
You use PHP by inserting PHP code inside the HTML that makes up your website. When a client (anybody on the web) visits a web page that contains this code, your server executes it. That’s why you need to install your own server in order to test PHP locally — the server is the brain here, not your browser. Users don’t need any special plug-ins or anything to see your PHP in action — it gets to the end user as regular old-fashioned HTML.
PHP is a scripting language, like HTML. That means that code does not need to be compiled before it gets used — it gets processed on the fly as necessary.
Before we dig in, you should know about a site called PHP.net. PHP is an open-source language, and PHP.net is its control center, with extensive reference material about the language and tips sent in by users across the globe. PHP.net has exceptional, deep information about the language, but it can be a little cryptic for the newcomer. We’ll look more closely at how to use PHP.net at the end of this tutorial.
So, what kinds of things can PHP do? Welllll … it can:
  • take info from web-based forms and use it in a million ways (store it in a database, create conditional pages depending on what the forms said, set cookies for later, send e-mail, write your mom on her birthday);
  • authenticate and track users;
  • run threaded discussions on your site;
  • serve different pages to people using different browsers or devices;
  • publish an entire website using just a single layout template (server-side includes-style);
  • serve XML pages.
But before we can get to the specific uses of PHP, we need to start with a quick preview of the building blocks of PHP, beginning with a sample script. This example script is titled “chickenman.php.” When called by a web browser, it would simply read, “I am the CHICKEN MAN!”
1<?php
2
3
4
5 print ("I am the CHICKEN MAN");
6
7
8
9 ?>
The  ?php and tags start and end a PHP script, and your meat goes in the middle. Got that? Good! Now let’s walk through the basic rules you need to know to before you can write your first PHP script.

What you’ll need

Before we begin, you will need to install a server on your own machine in order to test your PHP scripts locally. You can install WampServer for Windows machines from http://www.wampserver.com/en/ In order to have a Localhost machine. If you’re using a Mac you can get MAMP from http://www.mamp.info.
If you have space on a web server which supports PHP, you can also test your PHP there, but this is kind of a pain because it means you’ll need to FTP your files or telnet in every time you want to change something.

Steps


The Basics

The code itself fits right inside a page’s HTML, and like HTML it is made up of plain ol’ text. So a page that displays the words “I am the CHICKEN MAN!” message would sit inside an HTML page named something.php, like this:
01<html>
02 <head>
03 <title> Chicken Man Example </title>
04 </head>
05 <body>
06 <font color="red">My PHP code makes this page say:</font>
07 <p>
08 <?php
09 print ("I am the CHICKEN MAN");
10 ?>
11 </p>
12 </body>
13 </html>
See how that works? The HTML is rendered as regular HTML, but everything inside the  ?php and tags gets processed as PHP.

Basic Syntax

It’s time to write your own first PHP script. The basic rules of PHP are as follows:
Naming Files In order to get a PHP script working, the file it’s in or the file that it calls to do the heavy lifting must end in .php (earlier versions used the file extensions .php3 and .phtml). Like HTML, your files are saved as plain text.
Comments It’s important to get in the habit of leaving notes about your code with the comment tags so that months down the road you can make sense of what you were trying to make your script do. The way you set comments apart from your code (that you don’t want displayed or executed) is with either “//” at the beginning of each line, or surrounded by “/*” and “*/” if you want to comment out several lines:
01<?php
02
03// This will be ignored. Note to self:
04
05// Pick up ice cream, cake, and balloons.
06
07print ("I am the CHICKEN MAN");
08
09/*
10
11This, too, will be ignored.
12
13Hey, and don't forget
14
15the spanking machine!
16
17*/
18
19?>

Code Syntax

Start of Code Every piece of PHP code begins with “<?php” (or the abbreviated “<?” if your server is configured to handle that).
End of Code The way to signify that the PHP code is finished is by adding “?>” at the end.
Every Chunk With a few exceptions, each separate instruction that you write will end with a semicolon.
Parentheses The typical function looks like this …
1print ( );
… where “print” is the function and the stuff that the function works on sits inside the parentheses, with a semicolon to finish it off. (Just to confuse you, “print” is the exception that also works without parentheses.) By the way, echo () is the same as print ().
Much like HTML, the actual formatting of your PHP code (where you put spaces, line breaks, etc.) will not affect the outcome except those parts of the code that tell a web browser how to display your page. So this piece of code …
01<?php
02
03
04
05    print ("I am the CHICKEN MAN");
06
07
08
09    ?>
10
11
12
13... is effectively identical to:
14
15
16
17    <?php print ("I am the CHICKEN MAN"); ?>
Like more complicated HTML, it behooves you to use white space and tabs in your code to make the code more understandable.
Ready to write your first script? Let’s go.

Your First Script

OK, so write your first script already! Copy the following script, but put whatever you want inside the quotation marks. “Print” here means print to the screen of a web browser when you open the file:
01<html>
02
03<body>
04
05  <?php
06
07  print ("I am the CHICKEN MAN");
08
09  ?>
10
11</body>
12
13</html>
Save the file with any name that has no spaces and ends in .php, and if you’ve installed a server on your own machine, you need to save the script somewhere inside the server’s root folder (on Windows this is typically in the “wwwroot” directory inside the “inetpub” directory on your C: drive).
The next step is to open the file in your browser. Since you need the server to run your PHP code, you have to open the file through a URL that finds the correct file through your web server. On Windows, your computer name is your root URL. My computer name is “rocketboy,” so to see the contents of my root directory, I type “http://rocketboy” into the Web browser and voila! I see the contents of my root folder. To open the file “chickenman.php” in a directory called “tests” inside the root directory, I’d type “http://rocketboy/tests/chickenman.php” and see my example.
If you’re testing on a PHP-able web server, FTP your files anywhere on your server and they should work when you open them through the URL.
Go on now and get your first script working. Then come back and we’ll have some fun. Together. (If you can’t get your first script working, look at our First Script Troubleshooting Guide.)

Error Messages

Fun, eh? Fun if it worked. If not — if you had an error in your script — you probably got an error message that looked something like this:
1Parse error:
2
3parse error in C:Inetpubwwwrootwebmonkey_articletest9.php
4
5on line 12
Error messages can be very useful and you’re bound to run into lots of them. You’ll get a message like this for every line in your script that has an error. For our purposes, all we really need to know is that there is something wrong with our code in line 12 of the document “test9.php,” so let’s look at that line and see if we can figure it out (good text editors like BBEdit have a function that lets you jump to any particular line). I always start by looking to see if my basic syntax is correct: did I leave out the closing tag, a line’s semicolon, quotation marks?

A Few More Statements

Let’s continue by adding to your test code from the last page to show a couple useful tools.
In the same code that you wrote before, drop in a couple more statements. As you see, you can gang up more than one PHP function inside the same opening and closing tags. My comments in the code explain what each part does:
01<html>
02
03 <body>
04
05 This text right here (or any HTML I want to write)
06
07 will show up just before the PHP code stuff.
08
09 <p>
10
11 <?php
12
13 // first, this $PHP_SELF thang is
14
15 // an environment variable that'll show the
16
17 // path from your root folder to this
18
19 // document itself, like /webmonkey_article/test3.php.
20
21 // I put this in just for fun.
22
23 // NOTE: This may only work if your server is Apache.
24
25 print "$PHP_SELF";
26
27 // next we have to "print" any
28
29 // HTML code we want the browser
30
31 // to follow to determine
32
33 // the layout of the results page.
34
35 // In this case, we're adding a <p> tag
36
37 // the <p> tags could have been put
38
39 // inside the same print statement as the
40
41 // "I am the CHICKEN MAN" text.
42
43 // between the $PHP_SELF text and the
44
45 // next bunch of stuff.
46
47 print ("<p>");
48
49 print ("I am the CHICKEN MAN");
50
51 print ("<p>");
52
53 /* This next "phpinfo" business outputs a long page that
54
55 tells you exactly how your version of PHP is configured.
56
57 This can be useful when troubleshooting problems down
58
59 the road */
60
61 phpinfo();
62
63 ?>
64
65 </p>
66
67 </body>
68
69 </html>
NOTE: Phpinfo will output a long page of info about your version of PHP. You don’t need to understand what it all means, I just wanted to show you that it’s there if you ever need it.


Parham PHP LessonsLesson 01 - Installing a Server
Lesson 02 - PHP Basics
Lesson 03 - Variable/array/function definitions
Lesson 04 - Setting and printing variables
Lesson 05 - Arrays
Lesson 06 - Operators
Lesson 07 - Control structure, if statement
Lesson 08 - Control structure, foreach loop
Lesson 09 - Control structure, while loop
Lesson 10 - Basics of user input
Lesson 11 - Using user input
Lesson 12 - Using functions
Lesson 13 - Creating custom functions
Lesson 14 - Variable scope and global declaration
Lesson 15 - Variable referencing
Lesson 16 - More function examples
Lesson 17 - Regular Expressions
Lesson 18 - Filesystem functions
Lesson 19 - String functions

No comments:

Post a Comment