Hi everyone. This is a series of lectures for PHP programming that I actually gave at a University. I have numbered every lecture with its number and its title. This is first lecture of this series. I have also uploaded the pdf version of this series in Download section of this website.

Today, We are going to look at :

  • What the PHP is?
  • Why PHP is so bloody great?
  • How to code properly in PHP?

Recommended Reading:
Web Database Application with PHP and MySQL, 2nd Edition  By David Lane, Hugh E. Williams[Download]

PHP History

PHP is a language for creating interactive web sites. It was originally called “Personal Home Page Tools” when it was created in 1994 by Rasmus Lerdorf to keep track of who was looking at his online CV.
Mid-1997: students Andi Gutmans and Zeev Suraski  redesigned the PHP language engine and wrote some of the most popular PHP modules. At that time PHP already had its own site, php.net,  run by the computer science community, and was powering thousands of Web sites.
What is PHP?
  • PHP originally stood for “Personal Home Pages”.
  • PHP is now a recursive acronym that stands for “PHP: Hypertext Preprocessor”.
  • PHP is a server-side scripting language, like ASP
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix, Oracle etc)
  • PHP is open source and free to download
  • PHP files many contain text, HTML tags and scripts
  • PHP files are returned to the browser as plain HTML
  • PHP files have extension of “.php”, “.php3” or “.phtml”
Why PHP?
  • PHP runs on different platforms (windows, Linux etc)
  • PHP is compatible with almost all servers used today (Apache, IIS etc)
  • PHP is FREE to download from official PHP resource: http://www.php.net
  • PHP is easy to learn and runs efficiently on the server side

What you need for PHP?

  • Install an Apache Server (web server) on a windows or Linux machine
  • Install PHP (server side scripting technlogy) on a windows or Linux machine
  • Install MySQL (database server) on a windows or Linux machine
  • And lots of Configuration work!!! :-(
Alternatively, Just download WAMP Server and install. It will not only installs Apache, MySQL and PHP on windows machine but will also configure these softwares. It provides you an easy to access interface to run and host PHP files.
Hello World in PHP
echo "Hello World" 

You can also use “print” command instead of “echo”.

PHP Tag Styles

XML Style:
print "this is XML style";

Short Style:

print "this is ASP style";

To use Short style, the PHP you are using must have “short tags” enabled in its config file. This is almost always the case.

Switching Modes

  <b>You are using Internet Explorer</b>
  <b>You are not using Internet Explorer</b>

PHP is C++ Style

PHP is very similar to C++. This is because C++ is top banana. As a consequence if you know java (also a c++ clone), C++ or indeed almost any other computer science language you pretty much already know PHP. However more than anything PHP is based on Perl.
Creating a PHP program
  • You can use any text editor. (we will use Dreamweaver)
  • Different Web server setups work in different ways. (we will use WAMP Server that bundles Apache, MySQL, PHP)
  • Create a new file in Dreamweaver and save it with .php extension.
  • Use <?php To open your code and ?> to close it.
  • Anything not in those tags is rendered as HTML.
  • Type your code and load it up in a web browser.
  • Any webpage that use even a single line of php and all other contents are in HTML must be saved with .php extension. Otherwise php code will not run.


  • Why do we go on about comments so much?
  • You can any of the following comment style in php.
// C style comment
# Perl style comment
C++ multi line comment
Hello World … Once again!… (Mixing HTML and PHP)
Hello World in HTML
echo "Is it PHP your looking foooor…\n";
// This is comment
This is HTML again

PHP is for Lazy People

Yup, you know that means you!
Basic data types
  • numbers (integers and float. Holds 4 bytes of space)
  • strings  (Double-quoted “abc“ and single-quoted ‘abc’ )
  • booleans (true, false )
  • Arrays
  • Objects
Dynamic typing
  • Don’t have to declare types
  • Automatic conversion done

Variables in action

$x = false;          // boolean
$x = true;
$x = 10;          // decimal
$x = 1.45;          // Floating point
$x = 'Hello World';      //  Hello World
$x = "mmm\"oo'oo";      //  mmm"oo'oo
$y = &$x;          // Reference
$x[1] = 10;          // array of decimals
$x["name"] = “jimbo";     // associative array
$x[2]["lala"] = "xx";     // a two dimensional array

Variable substitution

  • Variable substitution provides a convenient way to embed data held in a variable directly into string literals.
  • PHP examines, or parses , double-quoted strings and replaces variable names with the variable’s value.
  • The following example shows how:
$number = 45;
$vehicle = "bus";
$message = "This $vehicle holds $number people";
// prints "This bus holds 45 people"
print $message;

Type conversion

Conversion between types can be pure (forced) or dirty (automatic)

Forced casting (pure):

$bool = true;
print (int)$bool;
Automatic Type Juggling (dirty):
$x = "100";
$x++; // $x is now 101
Check Point:

If $x = “12” and $y = “13”. What will be the output for $x . $y and $x + $y?

Variable Scope

The 3 basic types of scope in PHP is:
  • Global variables declared in a script are visible throughout that script, but not inside functions. Declared as: global $x;
  • Variables used inside functions are local (limited) to the function. By default a variable inside a function is local. You can also define a local variable as: local $x;
  • Variables used inside functions that are declared as global refer to the global variable of the same name

Numerical functions – as per normal

$a = 1 + 1;  // sets $a to 2
$a  += 4;  // adds 4 to $a
$a++;  // adds 1 to $a


$a = 10 - 5;  # sets $a to 5
$a -= 6;  # subtracts 6 from $a
$a--;  # subtracts 1 from $a
$a = 2 * 3;  // sets $a to 6
$a *= 10;  // multiplies $a by 10
$a = 10 / 3;  // sets $a to 3.3333
$a /= 2;  // halves $a
$a = 10 % 3;  // sets $a to 1
$a %= 2;  // sets $a to modulus 2
// of itself

PHP Operators

  • +   Addition
  • -   Subtraction
  • *   Multiplication
  • /   Division
  • %   Modulus
  • &   And (00001101 & 00000111 results 00000101)
  • |   Or   (00001101 | 00000111 results 00001111)
  • ^   Xor  (00001101 ^ 00000111 results 00001010)
  • .   add string (concatenation)
  • <<   Shift left (00001101  results 00011010)
  • >>   Shift right(00001101 results 00000110)

Deja Vu

Just for the record what’s the same as C++/Java?

  • For Loops
  • While Loops
  • If Statements
  • Break, Continue, Exit, Switch, etc.
The main concepts which differ in syntax are:
  • Functions
  • Classes
if statements
if (some test)
// code here
else if (some other test)
// code here
// if neither test met, do this

If – exists

if (!empty($a))
print "\$a exists";
Foreach loops – Useful things
$a = array (1, 2, 3, 17);
foreach ($a as $v)
    print "Current value of \$v: $v.<br>";
// values and keys
$a = array (1, 2, 3, 17);
foreach($a as $k => $v)
     print "\$a[$k] is $v<br>";

Mixing up types in Arrays

$arr = array(1,'foo',1.57,'cat'=>'mouse','dog'=>'mailman');
 foreach( $arr as $k=>$v )
     print "\$arr[$k] = $v <br> \n";

PHP Functions

    • Functions in PHP are not case sensitive.
    • Be careful of this because variable naming is case sensitive
function my_function()
print "My function was called";
    • Functions can be created anywhere in your PHP code
    • However good style demands they should always be at the top of your code

Passing Parameters

    • As normal you don’t have to specify the types of your parameters.
    • Be careful of this because variable naming is case sensitive.
function display_table($data)
print "<TABLE border=1>";
print "<TR><TH>Key</TH><TH>Value</TH></TR>";
foreach($data as $key=>$value)
   print "<TR><TD>$key</TD><TD>$value</TD></TR>";
print "</TABLE>";
$arr = array(1,'foo',1.57,'cat'=>'mouse','dog'=>'mailman', "abc");

Defaults & Passing by Reference

    • As with other languages you can set defaults. PHP requires that you do specify the correct number of parameters in a call.
  • Equally you don’t have to pass by value – an ampersand will mean the variable is passed by reference and so any changes to it are global:
function increment(&$value, $amount=1)
$value = $value + $amount;

Returning Stuff

  • The keyword return stops the execution of a function.
  • Your program can contain more than one return statements, the first one encountered in the program flow will end the function.
  • You don’t need to specify a return type in the function declaration…
function larger($x, $y)
  if ($x > $y)
     return $x;
  if ($x < $y)
     return $y;
  if ($x == $y)
     return "x and y have the same value";

Object Orientation

  • Much better in PHP 4 and 5 than its previous version
  • No real data encapsulation.
  • Don’t worry about it. Almost everything you need can be done using functions.
class classname
  var $attribute1;
  var $attribute2;
  function method1(){ }


  • PHP is easy. It does most things for you.
  • From now on we will be looking at why it is Good as opposed to simple.
  • Its tricks, features and functions are excellent.

Next>> Lecture – 2. Form Handling in PHP

Tagged with: PHP

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.