Introduction to PHP

A PHP file may contain text, HTML tags and scripts. Scripts in a PHP file are executed on the server.

What You Should Already Know

Before you continue you should have a basic understanding of the following:

·  HTML

·  Some scripting knowledge

If you want to study these subjects first, find the tutorials here (http://www.w3schools.com/default.asp).

What is PHP?

·  PHP stands for PHP: Hypertext Preprocessor

·  PHP is a server-side scripting language, like ASP

·  PHP scripts are executed on the server - unlike javascript and java applets which run on the user’s machine.

·  PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

·  PHP is an open source software (OSS)

·  PHP is free to download and use

What is a PHP File?

·  PHP files may contain text, HTML tags and scripts

·  PHP files are returned to the browser as plain HTML

·  PHP files have a file extension of ".php", ".php3", or ".phtml"

What is MySQL?

·  MySQL is a database server

·  MySQL is ideal for both small and large applications

·  MySQL supports standard SQL

·  MySQL compiles on a number of platforms

·  MySQL is free to download and use

PHP + MySQL

·  PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform)

Why PHP?

·  PHP runs on different platforms (Windows, Linux, Unix, etc.)

·  PHP is compatible with almost all servers used today (Apache, IIS, etc.)

·  PHP is FREE to download from the official PHP resource: www.php.net

·  PHP is easy to learn and runs efficiently on the server side

Where to Start?

·  Install an Apache server on a Windows or Linux machine

·  Install PHP on a Windows or Linux machine

·  Install MySQL on a Windows or Linux machine

An easier Way to have php at home

·  Try using pre-compiled versions of Apache/MYSQL/PHP for Windows (WAMP), like Xampp (http://www.apachefriends.org/en/xampp.html)

What should I do First ? (Exercise 1 )

·  Use google search (or try http://www.hotscripts.com/) to find sources of freeware php code. Find a simple php application (ie one that doesn’t require MYSQL) and install it to your webspace under the directory Exercise 1. A useful example may be a script that allows you to upload files to your web directory from home.

PHP Syntax

You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain HTML. This is because the scripts are executed on the server before the result is sent back to the browser.

This is useful if you don’t want people visiting your site to know how you coded your pages.

Basic PHP Syntax

A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.

On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.

However, for maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

<?php
?>

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.

Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser:

<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>

Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.

There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World".

Comments in PHP

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.

<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>

What should I do Next ? (Exercise 2 )

Create a new web page and place the following code inside it. Save the file as phpinfo.php What does the code do ?

<?php
phpinfo();
?>

PHP Variables

Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script.

Variables in PHP

Variables are used for storing a values, like text strings, numbers or arrays.

When a variable is set it can be used over and over again in your script

All variables in PHP start with a $ sign symbol.

The correct way of setting a variable in PHP:

$var_name = value;

New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work.

Let's try creating a variable with a string, and a variable with a number:

<?php
$txt = "Hello World!";
$number = 16;
?>

What should I do Next ? (Exercise 3 )

Create a new web page and place the following code inside it. Save the file as caligula.php What does the code do ?

<html>
<?php
$str = “<textarea rows=\”5\” cols=\”48\”>
?>
</html>

PHP is a Loosely Typed Language

PHP is a loosely typed language. In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. Most programming languages require you to do this so that the computer knows how much memory to set aside for each variable. For example, in Visual BASIC you would declare a variable using the DIM function :

DIM HouseNumber as INTEGER
DIM Surname as STRING

In PHP, however, a variable does not need to be declared before being set

In the example above, you see that you do not have to tell PHP which data type the variable is.

PHP automatically converts the variable to the correct data type, depending on how they are set.

In PHP the variable is declared automatically when you use it.

Data types

There are eight data types in PHP:

1.  Integer / The integers (from the Latin integer, which means with untouched integrity, whole, entire) are the set of numbers consisting of the natural numbers including 0 (0, 1, 2, 3, ...) and their negatives (0, −1, −2, −3, ...). In non-mathematical terms, they are numbers that can be written without a fractional or decimal component, and fall within the set {... −2, −1, 0, 1, 2, ...}. For example, 65, 7, and −756 are integers; 1.6 and 1½ are not integers.
2.  Double / Double precision is a computer numbering format that occupies two adjacent storage locations in computer memory. A double precision number, sometimes simply called a double, may be defined to be an integer, fixed point, or floating point.Modern computers with 32-bit stores (single precision) provide 64-bit double precision. Double precision floating point is an IEEE 754 standard for encoding floating point numbers that uses 8 bytes.
3.  Boolean / The Boolean datatype, sometimes called the logical datatype, is a datatype having one of two values: one and zero (which are equivalent to true and false, respectively).
4.  String
5.  Object
6.  Array
7.  Null
8.  Resource

The null data type represents a variable that has no value. The only value in the null data type is NULL.

Variables of the "resource" type represent references to resources from external sources. These are typically created by functions from a particular extension, and can only be processed by functions from the same extension. Examples include file, image and database resources.

Arrays support both numeric and string indices, and are heterogeneous. Arrays can contain elements of any type that PHP can handle, including resources, objects, and even other arrays. Order is preserved in lists of values and in hashes with both keys and values, and the two can be intermingled. Objects can syntactially be used as Arrays.

Variable Naming Rules

·  A variable name must start with a letter or an underscore "_"

·  A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )

·  A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Reserved Words

Here is the list of PHP reserved words, usually constants and predefined variables. You won't find any functions here, but rather language constructs. You shouldn't try to use such names as variable, function, constant or method names, as it will surely lead to confusion.

and / E_PARSE / old_function
$argv / E_ERROR / or
as / E_WARNING / parent
$argc / eval / PHP_OS
break / exit() / $PHP_SELF
case / extends / PHP_VERSION
cfunction / FALSE / print()
class / for / require()
continue / foreach / require_once()
declare / function / return
default / $HTTP_COOKIE_VARS / static
do / $HTTP_GET_VARS / switch
die() / $HTTP_POST_VARS / stdClass
echo() / $HTTP_POST_FILES / $this
else / $HTTP_ENV_VARS / TRUE
elseif / $HTTP_SERVER_VARS / var
empty() / if / xor
enddeclare / include() / virtual()
endfor / include_once() / while
endforeach / global / __FILE__
endif / list() / __LINE__
endswitch / new / __sleep
endwhile / not / __wakeup
E_ALL / NULL