Web Application Development
Web Application Development
DEVELOPMENT
Contents:
Learning Unit1: PHP Fundamentals
Performance Criterions:
1. Definition of PHP and its basics
2. Installation and configuration of PHP server
3. PHP Syntax
4. PHP variables
5. Constants
6. PHP Datatypes
7. Operators
Learning Unit 2 – Implement
PHP logic
Performance Criterions:
1. PHP ─ Decision Making/ PHP-control Structures
2. PHP ─ Loop Types
3. Arrays
4. PHP-Functions
5. File processing
6. Errors and handling exceptions
Learning Unit 3 .PHP-Web Concepts
Performance Criterions:
1. Using HTML Forms
2. Browser Redirection Displaying "File Download" Dialog Box
3. PHP ─ GET and POST Methods
4. PHP ─ File Inclusion
Learning Unit 4 –Perform PHP and MySQL Database
Performance Criterions:
1. Connection and access of MySQL database.
2. Implementation of database CRUD operations.
• Display:
• Hello World!
Comments in PHP
A comment in PHP code is a line that is not executed as part of the program. Its purpose is
to be read by someone who is editing the code.
PHP supports three forms or ways of expressing a comment. Comments are useful for:
· To remind yourself what you did - Most programmers have experienced coming back to
their Comments, this remind you of what you were thinking when you wrote the code
· To let others understand what you are doing - Comments let other programmers
understand each of your steps
// This is a single-line comment
# This is also a single-line comment
/*
is a multiple lines comment block that spans over more than one line
*/
Case sensitivity in PHP
In PHP, all variables are case-sensitive.
In the example below, only the first statement will display the value of the $pet
variable (because $pet, $PeT, and $PET are treated as three totally different variables):
Example
<html>
<body>
<?php
OUTPUT:
$pet=“cat”; My animal is a cat
echo “My animal is a ” . $pet . “<br>”; Undefined variable PeT
echo “My animal is a ” . $PeT . “<br>”; Undefined variable PET
echo “My animal is a ” . $PET. “<br>”;
?>
</body>
</html>
In PHP, all user-defined functions, keywords, and classes are case-
insensitive.
In the example below, all two echo statements below are legal:
Example
<html>
<body>
<?php
echo “Hello World!<br>”;
ECHO “Hello World!<br>”;
?>
-</body>
</html>
White spaces
With HTML, whitespace is ignored between PHP statements. This is to mean that there can
be one line of PHP code, then 2 or more lines of blank space before the next line of PHP
code.
Here is an elaborated example
<html>
<head>
<title>My First PHP Page</title>
</head> </body>
<body> </html>
<?php Display:
Hello World!
echo “Hello World! <br>”;
Hello World!
echo “Hello
World!”;
?>
TOPIC 2: PHP VARIABLES
A variable is a means of storing a value, such as text string “Hello World!” or the integer
value 30. A variable can then be reused throughout your code, instead of having to type
out the actual value over and over again,in other words variables can be termed as
“containers” for storing information
In PHP you define a variable with the following form:
• $variable_name = Value;
just Like in mathematics
r=1
s=2
t=r+s
Example
<?php
$txt=“Hello world”;
the letters r,s hold the numbers 1,2 respectively.
From the expression t=r+s above, we can calculate the value of t to be 3.
In PHP these letters are called variables.
Example
<?php
$r=1;
$s=2;
$t=$r+$s;
echo $t;
?>
Example
<?php
$txt=“Hello world”;
?>
• Rules for naming PHP variables
• Variables with more than one word should be separated with
underscores. $my_variable
• · A variable starts with the $ sign, followed by the name of the
variable
• · Variable names are case sensitive ($r and $R are two different
variables)
• · A variable name cannot start with a number
• · A variable name must start with a letter or the underscore
character
• · A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9,
•
• PHP Variables Scope
In PHP, variables can be declared anywhere in the script. The scope of a
variable is the part of the
script where the variable can be referenced/used. PHP has three different
variable scopes:
• Local
• Global
• Static
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
Example
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is:
$x</p>"; ?>
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that
function:
Example
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
• PHP The global Keyword
The global keyword is used to access a global variable from within a function. To do
this, use the global
keyword before the variables (inside the function):
Example
<?php
$x = 5;
$y = 10;
function yTest()
{ global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
• PHP The static Keyword
Normally, 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. To do this, use the static keyword when you first declare the variable:
Example
<?php function myTest() { static $x = 0; echo $x;
$x++;
}
myTest();
myTest();
myTest(); ?>
Then, each time the function is called, that variable will still have the information it
contained fromthe last time the function was called.
Note: The variable is still local to the function.
* Constants
Constants are like variables except that once they are defined they cannot
be changed or undefined. Abconstant is an identifier (name) for a simple
value. The value cannot be changed during the script. A valid constant
name starts with a letter or underscore (no $ sign before the constant
name).
Note: Unlike variables, constants are automatically global across the
entire script. Create a PHP
Constant.
To create a constant, use the define() function.
Syntax: define(name, value, case- insensitive)
Parameters:
- name: Specifies the name of the constant
- value: Specifies the value of the constant
- case-insensitive: Specifies whether the constant name should be case-
insensitive.
Default is false The example below creates a constant with a case-sensitive
name:
Example
<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING; ?>
Constants are Global
Constants are automatically global and can be used across the entire
script. The example below uses a constant inside a function, even if it is
defined outside the function:
Example
<?php
define("GREETING", "Welcome to W3Schools.com!");
Function myTest() {
Echo GREETING;
}
myTest();
?>
Operators
Operators are used to perform operations on variables and values. PHP
divides the operators in the
following groups:
o Arithmetic operators
o Assignment operators
o Comparison operators
o Increment/Decrement operators
o Logical operators
o String operators
o Array operators
o Conditional Operator
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction,
multiplication etc.
Exercises
1. Write a program to print “Hello World” using echo.
2. Write a program to print “Hello PHP” using a variable .
3. Write a PHP program to check whether a number is positive, negative, or zero.
4. Write a simple calculator program in PHP using switch case with two numbers Entered from the
form .
5. Write a PHP program to check if a person is eligible to vote The minimum age required to vote is
18)
6. Display the largest number among three numbers entered from HTML form.
7. Write a program to calculate Electricity bill in PHP
Conditions:
• For the first 50 units – Rs. 3.50/unit
• For the next 100 units – Rs. 4.00/unit
• For the next 100 units – Rs. 5.20/unit
• For units above 250 – Rs. 6.50/unit
5kxhLYSB7z
1. Write a PHP program to check if a person is eligible to vote The
minimum age required to vote is 18).
2. Write PHP Program to Convert Celsius to Fahrenheit.
CONDITIONS:
celsius = (fahrenheit - 32) / 5/9
fahrenheit = (celsius * 5/9) + 32
Project1
Create an online voting system with the following features:
1. A form to register voters/Users (Idno, Names, Age,
Sex, Address, Phone)
2. A form to insert candidates (Idno, Names, Age, Sex,
Address, Phone,Position) in Mysql database.
3. A page to display registered candidates where
registered voters can pic one of them.
4. A report to display sorted candidates in descending
order based their counted (summed) votes.