Web Development Using PHP - Part 2
Web Development Using PHP - Part 2
www.bits-sa.com
Web Development using PHP 2
Table of Contents
1. PHP Block .................................................................................................................................................. 4
Example 1.1 ........................................................................................................................................... 4
1.1 Comments in PHP............................................................................................................................ 5
2. Declaring Variables in PHP ........................................................................................................................ 5
2.1 Naming Rules for Variables ............................................................................................................. 6
Example 2.1 ........................................................................................................................................... 6
2.2 The Concatenation Operator .......................................................................................................... 6
3. HTML Form handling using PHP ................................................................................................................ 7
Example 3.1 ........................................................................................................................................... 7
3.1 The $_GET ....................................................................................................................................... 7
3.1.1 When to use method="get"? ....................................................................................................... 7
3.2 The $_POST ..................................................................................................................................... 8
3.2.1 When to use method="post"? ..................................................................................................... 8
Example 3.2 ........................................................................................................................................... 8
Example 3.3 ........................................................................................................................................... 8
3.3 The PHP $_REQUEST ....................................................................................................................... 9
Example 3.4 ........................................................................................................................................... 9
4. PHP Sessions ............................................................................................................................................. 9
4.1 PHP Session Variables ................................................................................................................... 10
4.2 Starting a PHP Session................................................................................................................... 10
4.3 Storing a Session Variable ............................................................................................................. 10
4.4 Destroying a Session ..................................................................................................................... 11
5. PHP Operators......................................................................................................................................... 11
5.1 Arithmetic Operators .................................................................................................................... 12
5.2 Assignment Operators .................................................................................................................. 12
5.3 Comparison Operators .................................................................................................................. 13
5.4 Logical Operators .......................................................................................................................... 13
6. PHP if else statement .............................................................................................................................. 13
6.1 The if Statement............................................................................................................................ 14
Example 6.1 ......................................................................................................................................... 14
www.bits-sa.com
Web Development using PHP 3
www.bits-sa.com
Web Development using PHP 4
1. PHP Block
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 ?>.
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.
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 following example we’ll
be using echo statement to output the text "Hello World".
Create a web page (blank page) with index.php in your htdocs/training/lecture2/ directory.
Example 1.1
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Note: The file must have a .php extension. If the file has .html extension, the PHP code will not be
executed.
www.bits-sa.com
Web Development using PHP 5
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
When a variable is declared, it can be used over and over again in your script.
$var_name = value;
Let's try creating a variable containing a string, and a variable containing a number:
<?php
$txt="Hello World!";
$x=16;
?>
In PHP, a variable does not need to be declared before adding a value to it.
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 its value.
www.bits-sa.com
Web Development using PHP 6
A variable name can only contain alpha-numeric characters and underscores (a-z, 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 an underscore ($my_string), or with capitalization ($myString)
Now open your index.php and add following (bold) text to it.
Example 2.1
<html>
<body>
<?php
echo "Hello World";
echo “<br>”;
$txt="Hello World";
echo $txt;
?>
</body>
</html>
Hello World
Hello World
The concatenation operator (.) is used to put two string values together.
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
www.bits-sa.com
Web Development using PHP 7
If we look at the code above you see that we used the concatenation operator two times. This is
because we had to insert a third string (a space character), to separate the two strings.
Example 3.1
Now create another file user.php and add following (bold) text to it.
This will contains an HTML form with two input fields and a submit button
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
When a user fills out the form above and click on the submit button, the form data is sent to a PHP file,
called "user_reply.php".
Information sent from a form with the GET method is visible to everyone (it will be displayed in the
browser's address bar) and has limits on the amount of information to send.
http://localhost:100/training/lecture2/user_reply.php?name=test&gender=Male&education=Masters&submit=Su
bmit
Note: This method should not be used when sending passwords or other sensitive information!
www.bits-sa.com
Web Development using PHP 8
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can
be useful in some cases.
Note: The get method is not suitable for very large variable values. It should not be used with values
exceeding 2000 characters.
Information sent from a form with the POST method is invisible to others and has no limits on the
amount of information to send.
http://localhost:100/training/lecture2/user_reply.php
Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting
the post_max_size in the php.ini file).
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
Example 3.2
Now create another file user_reply.php and add following (bold) text to it.
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Example 3.3
Using short output notation (change the text in user_reply.php file as follows).
<html>
www.bits-sa.com
Web Development using PHP 9
<body>
Welcome <?=$_POST["fname"];?>!<br />
You are <?= $_POST["age"];?> years old.
</body>
</html>
Now open up your last assignment and display its user input as showed follows;
The $_REQUEST can be used to collect form data sent with both the GET and POST methods.
Example 3.4
<html>
<body>
Welcome <?=$_REQUEST["fname"];?>!<br />
You are <?= $_REQUEST["age"];?> years old.
</body>
</html>
4. PHP Sessions
A PHP session variable is used to store information about, or change settings for a user session. Session
variables hold information about one single user, and are available to all pages in one application.
www.bits-sa.com
Web Development using PHP 10
A PHP session solves this problem by allowing you to store user information on the server for later use
(i.e. username, shopping items, etc). However, session information is temporary and will be deleted
after the user has left the website. If you need a permanent storage you may want to store the data in a
database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The
UID is either stored in a cookie or is propagated in the URL.
Note: The session_start() function must appear BEFORE the <html> tag:
<html>
<body>
</body>
</html>
The code above will register the user's session with the server, allow you to start saving user
information, and assign a UID for that user's session.
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
www.bits-sa.com
Web Development using PHP 11
?>
</body>
</html>
Output:
Pageviews=1
In the example below, we create a simple page-views counter. The isset() function checks if the "views"
variable has already been set. If "views" has been set, we can increment our counter. If "views" doesn't
exist, we create a "views" variable and set it to 1:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
<?php
unset($_SESSION['views']);
?>
You can also completely destroy the session by calling the session_destroy() function:
<?php
session_destroy();
?>
Note: session_destroy() will reset your session and you will lose all your stored session data.
5. PHP Operators
Operators are used to operate on values.
www.bits-sa.com
Web Development using PHP 12
+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
www.bits-sa.com
Web Development using PHP 13
|| or x=6
y=3
! not x=6
y=3
Very often when you write code, you want to perform different actions for different decisions.
www.bits-sa.com
Web Development using PHP 14
if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if a condition is true and another
code if the condition is false
if...elseif....else statement - use this statement to select one of several blocks of code to be
executed
switch statement - use this statement to select one of many blocks of code to be executed
Syntax
The following example will output "Have a nice weekend!" if the current day is Friday:
Example 6.1
<html>
<body>
<?php
$d=date("D");
if ($d=="Wed")
{
echo "Have a nice weekend!";
}
?>
</body>
</html>
Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.
Syntax
if (condition)
{
www.bits-sa.com
Web Development using PHP 15
Example 6.2
The following example will output "Have a nice weekend!" if the current day is Wednesday, otherwise it
will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D");
if ($d=="Wed")
{
echo "Have a nice weekend!";
}
else
{ echo "Have a nice day!";}
?>
</body>
</html>
Syntax
if (condition)
{code to be executed if condition is true;}
elseif (condition)
{code to be executed if condition is true;}
else
{code to be executed if condition is false; }
Example 6.3
The following example will output "Have a nice weekend!" if the current day is Wednesday, and "Have a
nice Saturday!" if the current day is Saturday. Otherwise it will output "Have a nice day!":
<html>
www.bits-sa.com
Web Development using PHP 16
<body>
<?php
$d=date("D");
if ($d=="Wed")
{echo "Have a nice weekend!";}
elseif ($d=="Sat")
{echo "Have a nice Saturday!";}
else
{echo "Have a nice day!";}
?>
</body>
</html>
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the structure. If there is a
match, the block of code associated with that case is executed. Use break to prevent the code from
running into the next case automatically. The default statement is used if no match is found.
Example 6.4
<html>
<body>
<?php
switch ($x)
www.bits-sa.com
Web Development using PHP 17
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
www.bits-sa.com
Web Development using PHP 18
7 Resources
7.1 websites
http://www.w3schools.com/
7.2 Books
Professional PHP Programming by Wrox word press Ltd. ISBN: 81-7366-201-0 (chapter 1- 6)
www.bits-sa.com