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
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)
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 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
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:- Create a new file in your favorite editor
- Type some PHP code
- Save the file with a .php extension
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 |
|---|
|
|
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