Lab Manual 3 DataBase
Lab Manual 3 DataBase
Spring 2024
CS-244 Database Systems-Lab
Lab-2 Manual
Introduction to PHP Basics
Lab-2 Manual 2024
Introduction to Lab:
This lab introduces students the server-side scripting language. In the previous lab we have
studied the client-side scripting language. This Lab introduces the basic and fundamentals of
this programming language. The purpose of studying php in database course is to make
interactive and dynamic web pages. So, let’s get started!
1. Introduction to PHP
2. Request Response Cycle
3. PHP Syntax
4. PHP echo and Print statement
5. PHP variables
6. PHP Super Globals
7. PHP Arrays
8. PHP if/else/elseif Statement
9. PHP Loops
10. PHP User defined Functions
11. PHP Built-in Functions
12. PHP Include and Require
13. PHP Form Validation
Page-1
Lab-2 Manual 2024
1. Introduction to PHP
Where
• Server-side means that PHP scripts execute on the Web server, not within the
browser on your local machine.
• Cross-platform means that PHP scripts can run on many different operating systems
and Web servers. PHP is available for the two most popular Web server
configurations IIS and Apache.
• HTML embedded scripting language means that PHP statements and commands are
actually embedded in your HTML documents. When the Web server sees the PHP
statements in the Web page, the server executes the statements and sends the
resulting output along with the rest of the HTML. PHP commands are parsed by the
server much like Active Server Pages or Cold Fusion tags.
The basic syntax of PHP is similar to C, Java, and Perl, and is easy to learn. PHP is used for
creating interactive and dynamic web pages quickly, but you can do much more with PHP.
Page-2
Lab-2 Manual 2024
3. PHP Syntax
The default syntax starts with "<? php" and ends with "?>". A PHP scripting block can be
placed anywhere in the document.
Example:
<?php
Code here……
?>
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
In PHP the two basic constructs to get outputs are echo and print. Actually, echo() is not a
function, it is a language construct, therefore, you can use it without parentheses.
Syntax
<?php
?>
All the above echo commands simply display the corresponding string, here we have used
an additional html command <br> at the end of each echo statement to generate a line break
as \n cannot generate a line break in browser.
Page-3
Lab-2 Manual 2024
We can display string, variables with echo function, additionally, we can embedded html
commands into echo command. Here we have attached html paragraph element in various
form into echo.
Example:
<?php
echo "<p style= 'color: red;'> One line simple string in red
color </p>";
green color</p>";
?>
Page-4
Lab-2 Manual 2024
We can display string, variables with echo function, additionally, we can embedded html
elements into echo command. Here we have attached html table elements into echo.
Example:
<?php
$a=1000;
$b=1200;
$c=1400;
echo "<table border=1 cellspacing=0 cellpading=0>
<tr>
<th>Name </th>
<th>Salary </th>
</tr>
<tr>
<td style='color:blue;'>Salary of Mr. A</td>
<td>$a$</td>
</tr>
<tr>
<td style ='color:blue;'>Salary of Mr. B</td>
<td>$b$</td>
</tr>
<tr>
<td style ='color:blue;'>Salary of Mr. C</td>
<td>$c$</td>
</tr>
</table>";
?>
Page-5
Lab-2 Manual 2024
Syntax:
print(string $val)
Parameters:
Return Values:
Returns 1, always.
Print() is not actually a real function, it is a language construct like echo. There is some
difference between the two, echo is marginally faster compare to print as echo does not
return any value. Echo can accept multiple parameters without parentheses but print can
accept only one parameter.
<?php
string example<br>';
Page-6
Lab-2 Manual 2024
?>
All the above print commands simply display the corresponding string, here we have used
an additional html command <br> at end of each print statement to generate a line break.
5. PHP Variables
Variable is a symbol or name that stands for a value. Variables are used for storing values
such as numeric values, characters, character strings, or memory addresses so that they can
be used in any part of the program.
All variables in PHP start with a $ (dollar) sign followed by the name of the variable
A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any
number of letters, numbers, or underscores.
If a variable name is more than one word, it can be separated with an underscore (for
example $employee_code instead of $employeecode).
Page-7
Lab-2 Manual 2024
<?php
?>
Some predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope - and you can access them from any function, class or file without
having to do anything special.
Page-8
Lab-2 Manual 2024
• $_REQUEST
• $_POST
• $_GET
$_REQUEST is a super global variable which is widely used to collect data after submitting
html forms.
Here is an example:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
Page-9
Lab-2 Manual 2024
we can collect the data entered by the user in different fields using $_RQUEST. Suppose we
want to see what data have been entered by the user in the name field, then code to do that
will be:
<?php
$name=$_REQUEST['fname'];
?>
Note: when you hit the sign in button, you are redirected to the php file you have
mentioned in the action attribute of the form.
Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 =>
value3, ...)). This array holds key/value pairs, where keys are the names of the form controls
and values are the input data from the user.
Page-10
Lab-2 Manual 2024
Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means
that they are always accessible, regardless of scope - and you can access them from any
function, class or file without having to do anything special.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST method.
HTML CODE:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
PHP CODE:
<?php
Page-11
Lab-2 Manual 2024
$name = $_REQUEST['fname'];
$email = $_REQUEST['email'];
$rollno = $_REQUEST['rollno'];
?>
HTML CODE:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
Page-12
Lab-2 Manual 2024
</form>
</body>
</html>
PHP CODE:
<?php
$name = $_REQUEST['fname'];
$email = $_REQUEST['email'];
$rollno = $_REQUEST['rollno'];
?>
Page-13
Lab-2 Manual 2024
Note: GET should NEVER be used for sending passwords or other sensitive information! So,
Developers prefer POST for sending the form data.
7. PHP Arrays
An array in PHP is a collection of key/value pairs. This means that it maps values to
keys. Array keys (or indexes) may be either integers or string whereas values can be any
type.
In PHP there is two kinds of arrays : indexed array and associative array. The only difference
is that numeric values are used as 'keys' in indexed array start from zero (0) and in
associative array, strings are used as 'keys'. PHP does not differentiate between indexed and
associative arrays, therefore a PHP array may contain strings as well as integers as 'keys'.
<?php
$courses = array("Database Systems", "SDA", "SQA");
var_dump($courses);
?>
<?php
Page-14
Lab-2 Manual 2024
var_dump($course_teacher);
?>
or:
<?php
$course_teacher = array();
$course_teacher['Ahmed'] = "SDA";
$course_teacher['Zunaina'] = 'SQA';
var_dump($course_teacher);
?>
In the above example, Ahmed, Zunaina and Tayabba are the keys and SDA, SQA and
Database Systems are the values of the array $course_teacher.
Syntax:
if (condition)
execute statement(s) if condition is true;
else
execute statement(s) if condition is false;
9. PHP Loops
Looping statements in PHP are used to execute the same block of code a specified number of
times.
Page-15
Lab-2 Manual 2024
Syntax:
Example:
<?php
$courses = array("DB" , "SQA" , "SDA");
$i = 0;
$arrLength = count($courses);
while ($i < $arrLength) {
# code...
echo $courses[$i]."<br>";
$i++;
}
?>
Output of above Example in Web Browser:
Syntax:
Page-16
Lab-2 Manual 2024
code to be executed;
In its simplest form, the for statement is used when you know how many times you want to
execute a statement or a list of statements.
Parameters:
• init: Is mostly used to set a counter, but can be any code to be executed once at the
beginning of the loop statement.
• cond: Is evaluated at beginning of each loop iteration. If the condition evaluates to TRUE,
the loop continues and the code executes. If it evaluates to FALSE, the execution of the
loop ends.
• incr: Is mostly used to increment a counter, but can be any code to be executed at the end
of each loop.
Note: Each of the parameters can be empty or have multiple expressions separated by commas.
• cond: All expressions separated by a comma are evaluated but the result is taken from
the last part. This parameter being empty means the loop should be run indefinitely. This
is useful when using a conditional break statement inside the loop for ending the loop.
Example:
<?php
# code...
echo $courses[$i]."<br>";
?>
Page-17
Lab-2 Manual 2024
For every loop, the value of the current array element is assigned to $value (and the array pointer
is moved by one) - so on the next loop, you'll be looking at the next element.
Syntax:
foreach (array as value)
{
code to be executed;
Example:
<?php
$course_teacher = array('Ahmed' =>'SDA' ,'Zunaina'=>'SQA','Tayabba'=>'Database
Systems' );
?>
Page-18
Lab-2 Manual 2024
The End
Page-19