Friday, November 18, 2011

Web Development Using PHP: An Introduction

by eturo


PHP gives you the freedom to add advanced features to your website. The aim of this chapter is to give you an easy, yet thorough and accurate introduction to PHP. It starts from scratch but requires that you already have a good knowledge of HTML.

PHP can be used in many contexts:
·         discussion forums
·         polls
·         shops
·         SMS gateways
·         mailing lists, etc…

The only limitation with what you choose to do with PHP is your imagination. PHP is not hard to learn, but be aware that PHP is more sophisticated and demanding to learn than HTML. Therefore, patience in the process is a virtue.

What is needed?

1.       It is assumed that you already have a text editor and know how it is used.
2.       Next, you need access to a computer or a server that can run PHP. In contrast to HTML and CSS, PHP is not affected by which browser your visitors use, but by the type of server that's hosting your pages. This is because PHP is a server-side technology.

Lesson 1: What is PHP
Whenever anyone is learning PHP, the most common questions that first come up are: What is PHP? And how does it work?

¤  What is PHP?

Ø  PHP was originally an acronym for Personal Home Pages, but is now a recursive acronym for PHP: Hypertext Preprocessor
Ø  PHP was originally developed by the Danish Greenlander Rasmus Lerdorf, and was subsequently developed as open source
Ø  PHP is not a proper web standard - but an open-source technology.
Ø  PHP is neither real programming language - but PHP lets you use so-called scripting in your documents
Ø  PH page has file extension name .php that contains a combination of HTML tags and scripts that run on a web server

¤  How does PHP work?

The best way to explain how PHP works is by comparing it with standard HTML. Imagine you type the address of an HTML document (e.g. http://www.mysite.com/page.htm) in the address line of the browser. This way you request an HTML page. It could be illustrated like this:

 
As you can see, the server simply sends an HTML file to the client. But if you instead type http://www.mysite.com/page.php - and thus request an PHP page - the server is put to work:


The server first reads the PHP file carefully to see if there are any tasks that need to be executed. Only when the server has done what it is supposed to do, the result is then sent to the client. It is important to understand that the client only sees the result of the server's work, not the actual instructions.
This means that if you click "view source" on a PHP page, you do not see the PHP codes - only basic HTML tags. Therefore, you cannot see how a PHP page is made by using "view source".

Lesson 2: Servers


Ø  PHP is a server-side technology
o   Therefore, you need to have a server to run PHP. But it doesn't need to cost you anything to make this upgrade and there are several options for doing so.

Ø  Methods to run your PHP files or pages:

 

Option 1: Website on a hosted server


You can choose to have a website on a host that supports PHP.
·         if you don't already have a website on hosted server you can create a free account on 000webhost.com which supports PHP

Option 2: Install PHP on your computer


This option is only recommend for experienced computer users, but it can obviously be done. Here are links to downloads and installation guides:

·         Windows Installation Guide (http://php.net/manual/en/install.windows.php)
·         Mac Installation Guide (http://www.php.net/manual/en/install.macosx.php)
·         Linux Installation Guide (http://www.php.net/manual/en/install.unix.php)

Option 3: XAMPP

 

XAMPP is a program that makes it easy and possible to run the PHP directly on your computer without having to install PHP on our own.

XAMPP’s name is an acronym for:
·         X (to be read as "cross", meaning cross-platform)
·         Apache HTTP Server
·         MySQL
·         PHP
·         Perl




Lesson 3: Your first PHP page

From lesson 1 and 2, you now know a little about what PHP is, and you've installed (or have access to) a server. Now we are ready to begin making our first PHP page. We keep it simple and easy, but after you have gone through this lesson, you will understand much more about what PHP is and what you can do with it.

Basically, a PHP file is a text file with the extension .php which consists of:
  • Text
  • HTML tags
  • PHP Scripts

You already know what text and HTML tags are. So let's look a little more at PHP scripts.

¤  PHP Scripts
PHP Documentation Group has issued detailed documentation for PHP (http://php.net/manual/en/index.php). Throughout the tutorial, there will be many links to the documentation. The goal is that you become accustomed to looking up and finding answers to your questions. PHP is so extensive that you can't to learn all facets in this tutorial. But PHP is not difficult! On the contrary, PHP is often very similar to plain English.

Example: Hello World!

Start by making an ordinary HTML document, but name the file page.php and save it in the root of the site:

·         If you use XAMPP, the path for the root is "c:\xampp\htdocs\page.php" on your computer (which is now a server).
·         If you have a website on a host that supports PHP, you simply upload/ftp the file to your web host.

The HTML code should look like this:

              
   
As you probably remember from lesson 1, PHP is all about writing commands to a server. So let's write a command to the server.

1.       First, we need to tell the server when the PHP will start and end. In PHP you use the tags and ?> to mark the start and end for the PHP codes that the server must execute (on most servers it will be sufficient to use just as start tag, but is the most correct to use the first time PHP is used.)
2.       Now try to add the following simple code snippet to your HTML code:


     

                

When we look at the PHP document in a browser, it should look like this:



But it gets interesting when you look at the HTML code in the browser (by selecting "view source"):


Lesson 4: Working with time and dates


In this lesson, we will try to look at the many different options for working with time and dates in PHP. We went through some very simple examples in the previous lesson mostly to show you what PHP is. In this lesson, we will take a closer look at the date function.

 

¤  Time and date functions

With different parameters, the date function can return the current date / time in many different formats. Some of the most useful parameters are:
Ø  date("y")
-         returns the current year from a date - with today's date, it returns: 11
Ø  date("m")
-         returns the current month from a date - with today's date, it returns: 09
Ø  date("F")
-         returns the current month name from a date - with today's date, it returns: September
Ø  date("d")
-         returns the current day of the month from a date - with today's date, it returns: 26
Ø  date("l")
-         returns the name of the current weekday from a date - with today's date, it returns: Monday
Ø  date("w")
-         returns the current day of the week from a date - with today's date, it returns: 1
Ø  date("H")
-         returns the current hour from a time - with the current time, it returns: 02
Ø  date("i")
-         returns the current minute from a time - with the current time, it returns: 41
Ø  date("s")
-         returns the current second from a time - with the current time, it returns: 23

Example:

 
Output:
¤ Time and date function parameters
Here are the list of parameters of the date() function.
 
 
Lesson 5: Comments in PHP scripts
Comments can be used to write short explanatory
text in the script.
The server completely ignores the comments, and the comments do not affect the actual
functionality of the script.
It is quite easy to insert a comment. You simply start the comment with
 a double forward slashes: "//".
 
 
Happy coding!

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...