learn

introduction

category: Basics
Created by: Dr.Ferrous

introduction

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pinned 

What is PHP?

PHP is an open source, server-side web-scripting language that is compatible with all the major web servers (most notably Apache) and databases.

By the help of PHP code you can create dynamic website content. Dynamic Web sites are flexible applications than merely sites.

If you know HTML, but you have no experience in the field of programming, you can easily learn PHP and MySQL with the help of this course.

This course starts from the most simple, explaining the operations of the PHP language, and then move on to detail how to create PHP programs, how to load the PHP scripts, and to the final approach more complex items, such as SQL commands, working with MySQL database, using classes and OOP (Object Oriented Programming) in PHP.

Running PHP in Windows

In order to run PHP, you need a server, because PHP is a server-sided scripting language, you either have to get some web space with a hosting company that supports PHP, or make your computer pretend that it has a server installed.

This is because PHP is not run on your PC - it's executed on the server. The results are then sent back to the client PC (your computer).

First, you need to download the software. You can get it from here: http://www.wampserver.com

 

You have now downloaded and installed Wampserver. This will give you a server on your own PC (Windows users).

If the installation went well, you should have an new icon in the bottom right, where the clock is:

Writing the structure of a PHP script

Every PHP script contains two special lines (or PHP tags) that indicate to PHP server that the text contained between the two tags must be parsed as PHP instructions. Between these two lines will be written the PHP code.

To start writing a PHP script, open your text editor (Notepad++, Brackets, Sublime Text, Atom.io...) and insert these two lines:

This is a start code and displays nothing.

You can save your script (with ANSI or UTF-8 format) in a text file with the ".php" extension, in the "www" (for WampServer) folder, with a name that respect the following rules:

  • It is recommended that the file name contains only lowercase characters, digits and dashes. Using spaces, capital letters and other characters may cause problems on different operating systems.
  • The filename extension is ".php"
  • Choose a meaningful name that describes the function of your script, so, that you can quickly identify it after weeks or months from its creation.

Your saved PHP files should be in this folder (usually c:\wamp\www):

Display output data in a Web browser

PHP scripts execute three basic operations:

  • Get data from a user.
  • Perform data processing, may get access to data stored in files and databases and manipulate them.
  • Displaying data so the user can view them.

Let`s write a simple PHP script that displays a text in the browser window. Open your text editor and add the fallowing code:

Save the file with a name with the ".php" extension (for example "index.php"), in the proper directory of your Web server ("c:\wamp\www\").
To see the result, access in the browser the fallowing address: http://localhost/index.php


If everything is made correctly, the browser will displays "Hello World!".


Notice that the instruction begins with a word "echo" and ends with a semicolon (;).

Each code line in PHP must end with a semicolon (;), it is used to distinguish one set of instructions from another.
The "echo" statement sends output data to be displayed in the browser (There are two basic statements to output text with PHP: echo and print.).
Single (or double) quotes are used to delimit strings (a text expression), in our case 'Hello World!'.

PHP is an HTML-embedded scripting language. This means that you can combine (intermingle) PHP and HTML code within the same file. Also, PHP can generate (X)HTML code.

here is another PHP script that outputs a HTML code and it's placed inside a HTML document:

Writing Comments

A secondary but still important aspect to any programming is documenting your code.
In PHP, you can use // to make a single-line comment or /* and */ for multiline comment block.

Example: