WT Module 5
WT Module 5
By Pooja Shukre-Kulkarni
Table of Contents
Introduction to PHP
PHP Tags
PHP Variables
Data types
Operators
Conditionals
Control Structures
Iteration constructs
Using arrays
String manipulations and regular expressions
PHP Forms
Session Control in PHP
Designing and creating your web database
Accessing MySQL database from the web with PHP
Session Control in PHP
Introduction to PHP
The full form of PHP is Hypertext Preprocessor. It was
abbreviated previously as Personal Home Page.
Default Syntax
The default syntax starts with "<? php" and ends with
"?>".
Example:
<?php
echo "Default Syntax";
?>
Short open Tags
The short tags starts with "<?" and ends with "?>". Short
style tags are only available when they are enabled in
php.ini configuration file on servers.
Example:
<?
echo "PHP example with short-tags";
?>
Omit the PHP closing tag at the end of the file
It is recommended that a closing PHP tag shall be omitted
in a file containing only PHP code so that occurrences of
accidental whitespace or new lines being added after the
PHP closing tag, which may start output buffering
causing uncalled for effects can be avoided.
Example:
<?php
echo "PHP example with short-tags";
PHP Statement separation
In PHP, statements are terminated by a semicolon (;)
like C or Perl. The closing tag of a block of PHP code
automatically implies a semicolon, there is no need to
have a semicolon terminating the last line of a PHP
block.
Example
<?php
echo 'This is a test string';
?>
In the above example, both semicolon(;) and a closing
PHP tag are present.
Example:
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Output Variables
The PHP echo statement is often used to output data
to the screen.
Example:
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
OR
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
PHP is a Loosely Typed Language
In the example above, notice that we did not have
to tell PHP which data type the variable is.
PHP automatically associates a data type to the
variable, depending on its value. Since the data
types are not set in a strict sense, you can do
things like adding a string to an integer without
causing an error.
In PHP 7, type declarations were added. This gives
an option to specify the data type expected when
declaring a function, and by enabling the strict
requirement, it will throw a "Fatal Error" on a type
mismatch.
You will learn more about strict and non-
strict requirements, and data type declarations in
the PHP Functions chapter.
Scope of PHP Variables
PHP has three different variable scopes:
1. local
2. global
3. Static
myTest();
function myTest()
{
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
The ‘static’ Keyword
When a function is completed/executed, all of its
variables are deleted. However, sometimes we want a
local variable NOT to be deleted. We need it for a
further job.
Learn PHP
Study PHP at W3Schools.com
9
PHP Data Types
String : $x = "Hello world!";
Integer : $x = 5985;
Float (also called double) : $x = 10.365;
Boolean : $x = true; $y = false;
Array :$cars= array("Volvo","BMW","Toyota");
Object
NULL
Resource
Classes and Objects
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and
behaviors from the class, but each object will have different values for the
properties.
Let's assume we have a class named Car. A Car can have properties like
model, color, etc. We can define variables like $model, $color, and so on, to
hold the values of these properties.
When the individual objects (Volvo, BMW, Toyota, etc.) are created, they
inherit all the properties and behaviors from the class, but each object will
have different values for the properties.
If you create a __construct() function, PHP will automatically call this function
when you create an object from a class.
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . "
" . $this->model . "!";
}
}
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
PHP String Operators
Operator Name Example Result
<?php
$age = 62;
<?php
$age = 6;
$age = 6;
<?php
$favcolor = "red";
switch ($favcolor)
{
case "red”: echo "Your favorite color is red!";
break;
case "blue": echo "Your favorite color is blue!";
break;
case "green": echo "Your favorite color is green!";
break;
default: echo "Your favorite color is neither red, blue, nor
green!";
}
?>
PHP Loops
while - loops through a block of code as long as the
specified condition is true
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example: do….while loop
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x < 2);
?>
Output:
}
<?php
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Example: foreach loop
<?php
$colors
= array("red", "green", "blue", "yellow");
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
PHP Functions
PHP has more than 1000 built-in functions, and in
addition you can create your own custom functions.
<?php
function writeMsg()
{
echo "Hello world!";
}
Output:
Kedar Patil. Born in 1995
Meena Patil. Born in 2000
Seema Patl. in 2015
Array
An array is a special variable, which can hold more
than one similar type of values at a time.
In PHP, there are three types of arrays:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . "
and " . $cars[2] . ".";
?>
Example: Associative arrays
<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"
);
echo "Peter is " . $age['Peter'] . " years
old.";
?>
Example: Multi-dimensional arrays
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold:
".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold:
".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold:
".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold:
".$cars[3][2].".<br>";
?>
PHP Sessions
A session is a way to store information (in
variables) to be used across multiple pages.
Unlike a cookie, the information is not stored on
the users computer.
When you work with an application, you open it,
do some changes, and then you close it.
This is much like a Session.
The computer knows who you are. It knows when
you start the application and when you end.
But on the internet there is one problem: the web
server does not know who you are or what you do,
because the HTTP address doesn't maintain state.
Session variables solve this problem by storing user
information to be used across multiple pages (e.g.
username, favorite color, etc). By default, session
variables last until the user closes the browser.
https://www.slideshare.net/netgainssolutions/session
s-and-cookies