This tutorial is a work in progress!!! come back later.
This tutorial is a Quick PHP Learning Course. It shows you the basic PHP programming in a simple, easy to understand and to follow format.
1. Things to know before programming in PHP --------------------------------------------------------------- 1.1 What is PHP
PHP is a programming language suitable for developing Dynamic Web Applications/Websites. It is simple, easy to learn, fast to program scripting language for Web development. It is most used today for that purpose.
Here is the usual Hello World program written in PHP:
hello_world.php <?php echo "Hello World!" ?> Note that PHP is a Scripting Language which means it is an interpreted language, so the PHP would be interpreted at runtime by PHP interpreter.
There's also compiled languages, like C or C++ which are first compiled from source code, obtaining the object code that is then linkedited and then a binary file is created (for example .exe files in Windows). So PHP does not belong in this category of compiled languages so the execution time is a little bit slower than running a already compiled binary.
So make that distinction between scripting languages and compiled languages.
1.2 What is LAMP PHP runs many hardware platforms. To write PHP programs developers usually use Linux, BSD or other UNIXes, but for development purpose PHP runs on windows too. In your PHP development quest you will encounter the notion of LAMP which stands for Linux Apache MySQL and PHP which describe the popular platform for developing PHP application, fromed by GNU/Linux Operating System, Apache Web Server, MySQL Database Server and of course PHP Scripting Language.
1.3 Install and Configure PHP Configuration of PHP is a little bit different regarding what platform you install PHP on. Here is a quick tutorial for installation of PHP under FreeBSD. A tutorial for installing and configuring PHP under Linux will be available soon.
PHP installation and configuration in FreeBSD: http://www.freebsdonline.com/content/view/628/531/
1.4 Running a PHP script A PHP script (program) can be run from command line with command:
php program.php
where program.php is your PHP script.
Or it can be placed under document root of your server and accessed via your browser using URL
http://localhosts/program.php
or
http://www.example.com/program.php
if your PHP script is not on your local machine.
Important: When you run a PHP script from command line you can use (when printing strings) identation characters like '\n' or '\t'. When running the PHP script from a Web server you must use HTML codes instead. So, '\n' will not work, you must use <BR>.
2. General PHP --------------------- 2.1 Include, require and require_once Include() and require() statements are used to include PHP files. Let's say we have a main PHP file and we want to store some functions and variables in other file, for example in functions.php file. For our main program to see functions and variables from the functions.php file we must use include or require. So our main.php file would be:
main.php <?php include "functions.php"; # our main.php code goes here. ?>In our previous example, instead of include() we can use require(). The difference between these statements is that when we use require, the file function.php must exist on disk, otherwise the program will give an error and will exit. There's also require_once() statement which will check if file is already included in other php files that are included, and if yes, it will not include it again. This statement will also check if file exist on disk and if not, it will signal and error and exit running of the PHP code that follows this statement.
2. PHP Variables
Variable can be defined in PHP using $:
$variable1 = 100; # we've just defined an integer variable $str1 = "Hello World" # we've defined a string variable
You might notice that we did not define a variable type previously to asigning a value to the variable (like it is in other programming languages such as C/C++). So the variable type is set automaticaly when we asign a value to the variable.
Important! When writing code do not forget to begin the PHP code with <?php and end it with ?> .
2.1 Boolean Type We can define a boolean type variable with:
$a = True # We've defined a variable
2.2 Integer Type To define an integer type variable we asign an interger value to the variable:
$b = 100;
Then we can display value of that variable with:
echo $b;
2.3 Float Type To define a float type variable we asign a float value to that variable:
$c = 100.11;
Then we can display the value of $c variable with:
echo $c;
2.4 Strings To define a string variable in PHP we asign a string to that variable:
$string1 = "This is a string";
Then we can display the value of $string1 variable with:
echo $string1; 2.5 Arrays Arrays in PHP are pairs of keys/values. To define an array in PHP we use:
$arr1 = array("key1" => "value1", "key2" => "value2");
Next example defines an array with 3 key/value pairs and display the values: array1.php <?php $arr1= array("color1" => "red", "color2" => "green", "color3" => "blue"); echo $arr1["color1"], "\n"; echo $arr1["color2"], "\n"; echo $arr1["color3"], "\n"; ?> 2.6 Objects
3. PHP Constants
4. PHP Control Structures 4.1 Conditionals - if - else 4.2 Loops - for - while - do while - switch
5. Defining Functions in PHP - Defining a PHP function - Passing arguments
6. PHP Library Functions
7. Superglobals PHP build in variables Superglobals are PHP build in variables available in everywhere within the script. These variables, sometimes also called server variables are very usefull when doing Web programming.
$_SERVER
7. Cookies
8. Sessions
9. Error Handling
10. Useful variables from php.ini file
11. PHP Security |