Sunday, December 30, 2012

PHP Tutorial (Faq's) part-1

What Is PHP?

PHP (PHP: Hypertext Preprocessor) is a server-side scripting language intended to help web developers build dynamic web sites quickly.

How Does PHP Work?

PHP scripts are executed on the server, before the web page is displayed to the user (this is what we mean by "server-side"). The user only sees the end result, which consists of client-side markup and scripts (i.e. HTML, JavaScript, CSS etc). Therefore, the user/browser doesn't actually see any PHP code. If the user views the source code, all they would see is HTML, JavaScript, CSS etc - they wouldn't see any PHP code.
This happens because, whenever the server processes a file with the .php extension, it knows to look for PHP code. When it encounters the PHP code, it processes it. Generally, the same .php file will also have client side code such as HTML. The server knows to process the PHP bits and output the client-side bits. You, as the programmer, determine which pieces of HTML will be displayed and when. You do this using PHP code.

What Can PHP Do?

PHP enables you to build large, complex, and dynamic websites. PHP can also increase your productivity enormously, both in development time and maintenance time.
Using PHP, you can build websites that do things such as:
  • Query a database
  • Allow users to upload files
  • Create/read files on the server (for example, the files that your users upload)
  • Have a "member's area" (i.e. via a login page)
  • Have a shopping cart
  • Present a customized experience (for example, based on users' browsing history)
  • Much, much more

What Do I Need to Create PHP Code?

You can create PHP code using the same equipment you use when creating HTML. That is, a computer with the following software:
  • Text editor. For example, Notepad (for Windows), Pico (for Linux), or Simpletext (Mac). You could use a special HTML or PHP editor if you like but it's not needed.
  • Web Browser. For example, Internet Explorer or Firefox.

What Do I Need to Run PHP?

To run the PHP pages you create, you need a computer with the following software:
  • A web server (such as IIS, Apache etc)
  • PHP
If you don't have these installed, you have a couple of options (apart from giving up!). The next lesson will point you in the right direction.

PHP Installation

If you don't have PHP installed on your computer, here are your best options:
  • Download and install PHP
  • Find a hosting provider that supports PHP (most hosting providers do)
You may already have a PHP hosting provider. If this is the case, all you need to do is upload your PHP files the same way you would upload any HTML file. If you don't have a hosting provider, check out these shared hosting reviews for a comparison of some of the top shared hosting providers. You will also find links to dedicated hosting and VPS hosting reviews.

Downloading PHP

Even if you have a PHP hosting provider, I recommend that you install PHP on your own local environment anyway. That way, you'll become more familiar with PHP, plus, you should always have a development environment that is separate to your production environment.
You can download PHP from the PHP website here:
http://www.php.net/downloads.php
To assist with the installation, you can view the PHP documentation here:
http://www.php.net/docs.php (Just select your preferred language).
Or, you can jump straight to the English version of the installation notes here:
http://www.php.net/manual/en/install.php

Installing a Web Server

If your machine doesn't have a web server installed, you will need to install one before you install PHP. There are many different web servers available to choose from, so you need to choose which one you prefer. Two of the more popular web servers are:
  • Apache
  • Internet Information Services (IIS)
If you're using Windows Server or Windows XP, it's possible your machine already has IIS installed. You can check to see if you have a web server installed by looking under "Administration Tools" for "Internet Information Services". You can usually find IIS at either Start > Program Files > Administration Tools or Start > Control Panel > Administration Tools.
If it's not installed, you may find that you can add IIS as an optional Windows component. To do this:
  • Go to Start > Control Panel > Add or Remove Programs > Add/Remove Windows Components)
  • If you see the option "Internet Information Services (IIS), select it (if it's already selected it's already installed).
  • Click Next
This should install IIS for you. Once you've installed IIS, open a browser and type http://localhost. This should display the default IIS homepage. You can change this by replacing the files in the default directory (or changing the default directory location). The default directory is normally at C:\Inetpub\wwwroot. This is where you can place your .php files.


PHP Syntax

The PHP syntax is based on C, Java, and Perl, so if you've used any of those languages PHP will look familiar to you. Creating a PHP file is similar to creating an HTML file. In fact, most PHP files are a mixture of PHP code and HTML.

Creating a PHP File

To create a PHP file, simply do the following:
  1. Create a new file in your favorite editor
  2. Type some PHP code
  3. Save the file with a .php extension
The .php extension tells the web server that it needs to process this as a PHP file. If you accidentally save it with a .html extension the server won't process your PHP code and the browser will just output it all to the screen.
OK, so that sounds easy. My guess is that you already know how to create a new file and save it, so let's concentrate on the other bit - the "type some PHP code" bit.

Basic Code Syntax

Scripting Blocks

Every block of PHP code must start with <?php and end with ?>. The following example outputs the text "PHP is easy!" to the screen:
Code

Note: If your server supports it, you can leave off the php bit (so that it starts off like this <? echo...), but I'd recommend you keep it. This way, you won't run into any compatibility problems that you could have easily avoided.

Semi-Colons

You need to place a semi-colon (;) at the end of each line of PHP code. This tells the server that a particular statement has finished.

Comments

In the programming world, "comments" refer to small pieces of narrative within the code that can be useful in assisting other programmers interpret the meaning of the code. They can also be useful to yourself if you need to return to a piece of code many months (or years) after you'd written it. These comments aren't displayed to the user, instead, the server ignores them - they are purely for the programmers!
To write a comment in PHP, you prefix single line comments within two forward slashes (//) or, if the comment spans multiple lines, you need to open the whole block with a forward slash and asterisk (/*) then close it with an asterisk and forward slash (*/).
Example
Code

White Space, Carriage Returns, etc

You can use tabbing, spaces, carriage returns etc to indent and format your code - this won't cause any issues for the PHP interpreter. As long as you don't forget to close each line with a semi-colon.

Displaying the Output

To display PHP files in a browser, you need to type the full http path. For example, something like this: http://localhost/php_syntax_example.php. In other words, you can't view the file using your file system's path (like you can with HTML files). For example, you can't just type something like this: C:\\inetpub\wwwroot\php_syntax_example.php.
Using the http path means that you are accessing the file via the web server. The web server knows that any file with a .php extension needs to be processed by PHP.

No comments:

Post a Comment