Forum rules

Please read our Guide to Making Ozzu Tutorials if you would like to submit your own tutorials.

TUTORIAL: Learning PHP (Part 1)

  • Bogey
  • Ounce of 'Zu'
  • Genius
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 5397
  • Loc: Ozzuland
  • Status: Offline

Post March 14th, 2008, 11:28 pm

Learning PHP - Part 1

Introduction


This tutorial is aimed at people with no knowledge of PHP (and some in HTML) and who would want to learn what PHP is and to learn the wonderful language of PHP :)

What is PHP?


PHP stands for "PHP Hypertext Preprocessor". It is a server-side language that enables the programmer to do a lot of the functions the power of PHP enables you to.

Forums and blogs are created, implemented and used by PHP. People use PHP to create forms, and even hole web sites. A database driven dynamic web site. That is something to think about. Easy to create after a hard work of creating the site. Also leaves the person with euphoria of good accomplishment.

*PHP has being used by many people by the time of it's creation in and was named as PHP/FI which went all the way to it's 2nd stage, and then it was succeeded by PHP 3.0. Now people use the more modern PHP 5 with much more functions than the previous stages of PHP. Read the History of PHP To find out more of the history of PHP.

PHP is a free language that was made for the web so programmers could use it as they see fit anywhere and anytime.

Although PHP is a powerful language, it could be hacked if you use it the wrong way.

Objectives of this tutorial


The objective of these tutorials is to teach a complete beginner at PHP to learn the language of PHP.

Saving and Opening PHP Files


If you decide to create a PHP file provided in this tutorial, you would need to know how to be able to save and open the PHP file. You can code in Notepad,
but I usually like to use HTML-KIT with the PHP Plug-In installed. But the PHP editor (if you choose to use one) is up to your choice and preference. You can even use Notepad 2 with your own costume syntax highlighting.

Well, once you create a file, to save it you want to use the extension of PHP. If you use Notepad, you would want to change "File Type" to "All Files" and than save the file as "*.php" without the quotes (and the asterisk :roll: ).

To open the PHP file and see the results, you would need either of the two things...
  1. A host with PHP enabled
  2. A local server installed on your computer
For the second option (A local server installed on your computer0, you can use either WAMP or Apache HTTP Server, but to save time and frustration, I suggest you install WAMP since it is easy to install and use (In my opinion. I tried using Apache... got lost... I'm using WAMP without any errors :) ).

Once you install wamp, you would need to go (by default in Windows) to C:\wamp\www and create a folder in there. That folder could be named anything. But for the purpose of this tutorial, just name it as "phpTutorial" without the quotes. You would be saving your PHP files in that folder. Everything in the "www" folder would be accessible to you (meaning you would be able to view the contents using WAMP).

When you get PHP files in there you would be opening that file by going to your browser and typing "localhost" or "127.0.0.1" in the address bar and under projects clicking "phpTutorial" and then clicking on the PHP file(s)...

On your local server, PHP should be enabled by default.

Using PHP


Just like any other language, PHP has starting tags as well. They are...

You put all of the PHP tags inside of those tags. The simplest "tag" about PHP that you will learn right now would be to create comments in PHP files.

There are two ways you can create a comment in PHP. One way is for big lines of text and another one is for short description text.

Comments for big lines of text...
  1. <?php
  2. /* This is a comment that can hold big lines of text. By "lines" I mean multiple lines
  3. like this one. It is very handy if you want to put something descriptive about the PHP
  4. file that you are creating so you won't forget what that file is and what it is for. */
  5. ?>

Of course, the comments you create in the PHP file wouldn't be seeing by the public. Even if they check the source of your page. PHP comments are hidden from view entirely (and so is the code).

Comments for short text...
  1. <?php
  2. //This is a comment
  3. ?>

It is recommended to use the comment for big lines of text especially for that... for big lines of text. The other one is used only for one line of text and no longer. The comments are handy when you would need to know what code goes to what code and what they are for and what they do. It helps many programmers who are using comments.

Variables and echoes


By "echo" I mean to print the text you put into the code and make it viewable to the public.

PHP uses "echo" to paste text into the screen. You can also store the text into variables and than echo the variables. That comes handy when you need to reuse a line of text on your site over and over again that are universal and change all the time. That way, you edit the PHP file and change the text stored into the echo command and you would be able to change all of those reused words/text in one shot. Below is a simple example of storing a text into a variable and then echoing it.
  1. <?php
  2. //storing "Hello World" into $text
  3. $text = 'Hello World';
  4.  
  5. //Echoing $text to print "Hello World"
  6. echo $text;
  7. ?>

That would show "Hello World" on your screen when you open that PHP file in your browser.

There is another command that works the same way as echo. Exactly the same way. That is the "print" command. Just change the "echo" to "print" in the above example, and you would get the same result.

Explanation of the code...

The "$text" is the variable. The variable name is "text" and the variable name should also come after the dollar sign "$". In PHP the dollar sign tells the PHP parser that the text right after it, is the variable. The text in between " '...' " and the equal sign "=" is the text that should be stored in the variable, so it could reuse the same text throughout. You can echo the same variable more than once in a PHP file.

You don't have to store the text into a variable and echo the text. You can simply echo the text such as below.
  1. <?php
  2. //Echoing "Hello World"
  3. echo 'Hello World';
  4. ?>

As you might notice, all of the commands are closed with a semi-colon ";". That is to tell the PHP parser when to stop parsing the PHP of that particular function, string or command. If you don't close the PHP functions, strings or commands you would get a PHP error just as you would mess up your layout if you didn't close an HTML tag.

On echo command you don't need the equal sign. If you do use the equal sign, you would get an error and it won't work. You can try it out just to see what it would do.

You can also store HTML into variables and echo HTML tags as well... Below is a simple example of storing an HTML tag and echoing an HTML tag...
  1. <?php
  2. //storing the HTML into a PHP variable
  3. $html = '<table><tr><td>This is some text in a table</td></td></table>';
  4.  
  5. //echoing the variable with the HTML stored in it
  6. echo $html;
  7.  
  8. //echoing the HTML independently
  9. echo '<table><tr><td>This is some text in a table</td></td></table>';
  10. ?>

If your HTML has the "single-quotes" just like your PHP does you would need to escape those quotes with a backslash "\".
  1. <?php
  2. echo '<div style=\'text\'></div>';
  3. ?>

That is to tell the PHP parser that those quotes don't mean to end the PHP lines from being PHP or to start them. If you are using a different type of quotes for your PHP, than you don't need to escape them.
  1. <?php
  2. echo '<div style="text"></div>';
  3. ?>

The only reason you escape the quotes is when the HTML quotes are the same as PHP quotes that you are using. (You don't necessarily need to use the "single-quote" for PHP.

You can also store variables into variables if you like. There is no real use for that, but you can do it... below is a simple example of such thing...
  1. <?php
  2.   //Storing "some text" into a $variable
  3.   $variable = 'some text';
  4.  
  5.   //storing $variable into $variable2
  6.   $variable2 = $variable;
  7.  
  8.   //Echoing $variable2
  9.   echo $variable2;
  10.   //You would see "some text" (without the quotes) on your screen
  11. ?>


Passing variables from page to page


Sometimes that could get strenuous or too hard if you want to pass a variable over multiple pages. Like through 3+ pages. There are some solutions for that though, but before we get to it, let us set up a simple HTML form with a METHOD of "post" and an ACTION of "form.php".
  1. <form action="form.php" method="post">
  2. Name: <input type="text" name="Pname" />
  3. <input type="submit" name="submit" value="submit" />
  4. </form>

Once (and if) you finished typing that, save that file as "form.php" without the quotes and you would be ready to go with the PHP side of the form.

Usually, people call the "php side of the form" as the "form validation" so that is what we will call it here. But we will keep the validation simple and add to it as we go throughout the tutorial. We will first use what we know in the form validation, but before we do that, we have to learn how to insert the submitted data into a variable.
  1. <?php
  2. //inserting the name into a variable
  3. $name = $_POST['Pname'];
  4. ?>

"Pname" above is the name of the field that the user submitted. In the HTML form we wrote above, you would notice that the attribute "name" is "Pname". That is telling the PHP to look for an input or some data source with the name of "Pname". The name of it could be anything. So if you want, you can change the "Pname" to anything you like... if you do, don't forget to change PHP and HTML and make them the same.

The "$_POST" tells the PHP what kind of method you used. The two methods you can use are "get" and "post". "Get" sends the information through the URL and "post" sends the information to the url. You can echo the name that the person submitted with the echo statement...
  1. <?php
  2. //inserting the name into a variable
  3. $name = $_POST['Pname'];
  4.  
  5. //echoing the name
  6. echo $name;
  7. ?>

That is as simple as that. If you put that PHP in the same page as you put the HTML form in (should be form.php if you saved the file) you would be able to submit your name and see the result. You can have some text to go along with your name... such as below example...
  1. <?php
  2. //inserting the name into a variable
  3. $name = $_POST['Pname'];
  4.  
  5. //echoing the name
  6. echo 'Your name is: '. $name;
  7. ?>

You will notice the way that it is set up. The variable is behind the "single quotes" and behind the dot. The dot basically tells the PHP parser that there is more after the single quotes to parse. The reason that the variable is behind the single quotes is because the variable is not a string. Just about anything that is inside the single quotes, it means that it is a string. (Just to note, you can use regular quotes if you like " " ").

The same process would be used it you would like to pass that variable to another page. All you have to do is put the PHP into another page and save that page as a PHP and have that page's url put into the "action" attribute in the form. It is best practice to do it that way. To separate the PHP from HTML, a lot of programmers do it that way.

For example, here is form.php (could be form.html now, since there is no PHP in it).
  1. <form action="form2.php" method="post">
  2. Name: <input type="text" name="Pname" />
  3. <input type="submit" name="submit" value="submit" />
  4. </form>

And below is form2.php.
  1. <?php
  2. //inserting the name into a variable
  3. $name = $_POST['Pname'];
  4.  
  5. //echoing the name
  6. echo 'Your name is: '. $name;
  7. ?>

It is that simple. Not tough at all.

Oh, and if you want some text after the variable it would be the same way as you did it before the variable...
  1. <?php
  2. //inserting the name into a variable
  3. $name = $_POST['Pname'];
  4.  
  5. //echoing the name
  6. echo 'Your name is: '. $name .'We hope that you enjoy your stay here.';
  7. ?>


If...elseif...else statements


If...elseif...else statements are actually really easy to use and implement. There are many different commands you can use in them and operators to check if certain values or statements are true or false and what to do in each case.

To get a hang of else...elseif...else statement lets do the following simple example using this...
  1. <?php
  2. ///Simple else...elseif...else statement
  3.  
  4. //The variable $a is a
  5. $a = a;
  6.  
  7.  if($a == a)
  8. {
  9.    //if $a is a than echo the following...
  10.    echo 'It is the letter "a"';
  11. }
  12.  elseif($a == b)
  13. {
  14.    //if $a is b than echo the following...
  15.    echo 'It is the letter "b"';
  16. }
  17.  else
  18. {
  19.    //if $a is not a or b echo the following...
  20.    echo 'I can't figure what it is';
  21. }
  22. //You know what $a is... right?
  23. ?>

Of course, that code would echo "It is the letter "a"" because the variable "$a" has the letter "a" stored in it (after the equal sign).

Lets do a simple check if the form in our example has being filled in completely or left blank by using the command "empty()".
  1. <?php
  2. //Setting the variables
  3. $name = $_POST['Pname'];
  4.  
  5. //Checking if the input field has being filled in
  6. if(empty($name))
  7. {
  8.   //If the variable $name is empty, echo the following...
  9.   echo 'You need to fill in the input field with your name.';
  10. } else {
  11.   //If the variable $name is filled in, echo the following...
  12.   echo 'Your name is: '. $name;
  13. }
  14. ?>

As you will notice, the echo statement which tells the PHP parser to print (or echo) the name submitted is inside the if...else statement. You don't have to have the elseif statement in it. All it is, is just setting another scenario for the PHP parser. Else means the opposite of what if is describing.

The command "empty()" checks if the variable is empty of any information or not. The variable has the information stored in it that you have submitted in the form.

Everything that you want to do if the scenario proves to be true should go in brackets " {...} " (as in example). If you really need to, you can have an if statement inside an if statement, but in this tutorial you don't need to do that (yet).

This statement is used in many validators and is popular throughout the web. There are some short hands for this, but we will go into them later on in the series.

All the else...elseif...else statement says is, "if this is like that than say this, else if this is like those than say this, else just say that" or in other words "if this is like this, that say that, or if it's like that than say this, otherwise say this". Not a really hard statement to learn. One of the first PHP statements that I learned.

Hiding validation until SUBMIT button is pressed


You might notice that some text on your PHP validation may show even before you press "submit" and send in your values. That could be taken care of easily with some simple PHP script.
  1. <?php
  2.  if(isset($_POST['submit']))
  3.  {
  4.   //your form validation
  5.  }
  6. ?>

All that piece of code is saying if the submit button is set (isset) (or pressed) than show the form validation. Isset checks if the variable has anything stored in it, and if it does, than it would be parsed as TRUE, and in this example, if it is true, than it WILL show the information. So once you press the submit button, you are storing a bit of information in it, making the [u]isset[/b] true and because of that, you will be shown the form validation.

So, if you implement this code into our simple tutorial, the form validation would look like the following.
  1. <?php
  2.  if(isset($_POST['submit']))
  3.  {
  4.    //Setting the variables
  5.    $name = $_POST['Pname'];
  6.  
  7.    //Checking if the input field has being filled in
  8.    if(empty($name))
  9.    {
  10.      //If the input field (the variable) is empty, then echo...
  11.      echo 'You need to fill in the input field with your name.';
  12.    } else {
  13.      //Otherwise echo...
  14.      echo 'Your name is: '. $name;
  15.    }
  16.  }
  17. ?>

It is a good practice to keep the code organized and well commented. Makes it easier for you in the future to edit the PHP to fit your needs. We will go more into depth on form validation later on.

Includes and requirements


Including other file into each other is a rather simple thing to do. This way, you can include one file into another without using the dreaded HTML frames or iframes. Nobody needs those annoying frames or iframes. The only difference in include command is that it parses the information in the file that is being included into another file as it is part of the file that the other file is being included into (:lol: understand it?).

Lets take the following example for a simple explanation.
  1. <?php
  2. //including text.html
  3. include('text.html');
  4. ?>

Let say that text.html is being included into index.php. Lets say that the source code for index.php is...
  1. <html>
  2. <head>
  3. <title>My Index</title>
  4. </head>
  5. <body>
  6. <div style="background-color: #CECECE;">
  7. <p>My Text... blah blah blah</p>
  8. </div>
  9. <?php
  10. //including text.html
  11. include('text.html');
  12. ?>
  13. </body>
  14. </html>

And the source code for text.html would be...
  1. <div style="background-color: #FFCC00;">
  2. <p>Some more bits of my text... blah blah blah</p>
  3. </div>

This thing is also useful in forms. This way you can have the validation show up in the same page that your form is on but the validation is actually on a different page. Lets implement this on our form example. Let the source for form.php be...
  1. <?php
  2. include('form2.php');
  3. ?>
  4. <form action="form.php" method="post">
  5. Name: <input type="text" name="Pname" />
  6. <input type="submit" name="submit" value="submit" />
  7. </form>

And lets have the following source for form2.php...
  1. <?php
  2.  if(isset($_POST['submit']))
  3.  {
  4.    //Setting the variables
  5.    $name = $_POST['Pname'];
  6.  
  7.    //Checking if the input field has being filled in
  8.    if(empty($name))
  9.    {
  10.      //If the input field (the variable) is empty, then echo...
  11.      echo 'You need to fill in the input field with your name.';
  12.    } else {
  13.      //Otherwise echo...
  14.      echo 'Your name is: '. $name;
  15.    }
  16.  }
  17. ?>

As you might notice, the form ACTION is form.php but the validation is on form2.php. Why? Well, we are including the PHP into form.php and we would want the form validation to be shown where the HTML form is... for ease to the user. This way, users can see which fields they have not filled in correctly and fix it while looking at the mistakes they have made.

There are other simple and similar ways to do the same ways, yet they have a little bit different "features" to them. They are...
  1. include
  2. include_once
  3. require
  4. require_once
We talked about include, so now let's talk about include_once. Generally, include_once is the safest one to use if you don't really need that file for security purposes. It would include the file without any errors. By that, I mean if you accidentally included the same file twice into the same file it would include it only once and won't bring up any errors. But if you do that with include it would include the file in twice and will bring up an error without even showing the result of "double inclusion".

The same rules go with require and require_once except that this time, if the file that it is supposed to require is not found, the page will not load and an error would be brought up. Everything in front of the require or require_once would be shown but everything behind it would not. That is if the file that is supposed to be required is not found.

At requirement, it includes it as well... if you put require and include at the same time, it would require it (and include because it is require) and include it so you will have two of the same things right by each other.

Conclusion


In conclusion of this tutorial, you learned how to code a simple form and how to create a simple form validation for it.

As you see, PHP is a powerful language that empowers you to do many things with it if you learn to use it well.

--------------------------------------------------

Go to part 2 ->

If you got any suggestions, please PM me :)
Guys, I need help with Wedevoy.com here. Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 14th, 2008, 11:28 pm

  • yomi
  • Newbie
  • Newbie
  • No Avatar
  • Joined: 02 Apr 2008
  • Posts: 14
  • Status: Offline

Post April 23rd, 2008, 2:20 am

hi

came across thiese library fxns in a script for paginating and i am trying to understand what they mean and what they do.

include 'library/config.php';
include 'library/opendb.php';
  • panther786
  • Newbie
  • Newbie
  • No Avatar
  • Joined: 19 Apr 2008
  • Posts: 6
  • Status: Offline

Post May 1st, 2008, 2:29 am

this is a useful information for us.
thank you so much.
now we can do some thing with this
  • markusPHP
  • Beginner
  • Beginner
  • User avatar
  • Joined: 23 Mar 2008
  • Posts: 44
  • Loc: york, england
  • Status: Offline

Post May 18th, 2008, 3:45 am

yomi wrote:
hi

came across thiese library fxns in a script for paginating and i am trying to understand what they mean and what they do.

include 'library/config.php';
include 'library/opendb.php';


Config will be where your database connection is

And opendb (open database) is where your database is selected.

Post Information

  • Total Posts in this topic: 4 posts
  • Moderator: Moderator Team
  • Users browsing this forum: No registered users and 1 guest
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2008. Driven by phpBB © 2001-2008 phpBB Group.

 
 
 

Need a pre-made web design for your website?

Check out our templates here: Ozzu Templates