0% found this document useful (0 votes)
3 views9 pages

Lab Manual 2 DataBase

The Lab-2 Manual for CS-244 Database Systems introduces students to PHP, a server-side scripting language essential for creating dynamic web pages. Key topics include PHP syntax, variables, control structures, and form handling, with practical examples provided throughout. By the end of the lab, students will understand server-side programming, conditional structures, and data handling in PHP.

Uploaded by

www.ahsanali3k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab Manual 2 DataBase

The Lab-2 Manual for CS-244 Database Systems introduces students to PHP, a server-side scripting language essential for creating dynamic web pages. Key topics include PHP syntax, variables, control structures, and form handling, with practical examples provided throughout. By the end of the lab, students will understand server-side programming, conditional structures, and data handling in PHP.

Uploaded by

www.ahsanali3k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

GIFT School of Engineering

and Applied Sciences

FALL 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!

The main topics cover in this lab includes:

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

Objective of this Lab

At the end of this lab students shall be able to learn

• Understand how server-side programming works on the web.


• PHP Basic syntax for variable types and calculations.
• Creating conditional structures
• Storing data in arrays
• Using PHP built-in functions and creating custom functions
• Understanding POST and GET in form submission.
• How to receive and process form submission data.

Page-1
Lab-2 Manual 2024

1. Introduction to PHP

PHP, an acronym for Hypertext Preprocessor, is a widely-used open source general-purpose


scripting language. It is a cross-platform, HTML-embedded server-side scripting language
and is especially suited for web development.

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.

2. Request Response Cycle

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.

4. PHP echo and Print Statement

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

echo (arg1, arg2... )

Example: Simple string display:

<?php

echo “One line simple string.<br>”;

echo “Two line simple string example<br>”;

echo “Tomorrow I \'ll learn PHP global variables.<br>”;

echo “This is a bad command : del c:\\*.* <br>”;

?>

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.

Output of above example in web browser:

Page-3
Lab-2 Manual 2024

4.1. PHP echo and HTML paragraph element:

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

// simple html statement.

echo 'One line simple string.<br>';

// display strings within paragraph with different color.

echo "<p> <font color=blue>One line simple string in

blue color</font> </p>";

echo "<p style= 'color: red;'> One line simple string in red

color </p>";

echo "<p style= 'color: green;'> One line simple string in

green color</p>";

?>

Output of above example in Web Browser:

Page-4
Lab-2 Manual 2024

4.2. PHP echo and HTML table element:

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

Output of above example in Web Browser:

4.3. PHP Print

Print is used to display a string.

Syntax:

print(string $val)

Parameters:

val: The input data.

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.

Example to display simple string with print():

<?php

print 'One line simple string.<br>';

print 'Two lines simple

string example<br>';

print 'Tomorrow I \'ll learn PHP global variables.<br>';

Page-6
Lab-2 Manual 2024

print 'This is a bad command : del c:\\*.* <br>';

?>

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.

Output of above example in Web Browser:

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.

5.1. Declaring PHP variables:

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).

‘$' is a special variable that cannot be assigned.

5.2. Pictorial presentation of PHP variable naming:

Page-7
Lab-2 Manual 2024

Example: Valid and invalid PHP variables:

<?php

$abc = 'Welcome'; //valid

$Abc = 'daraz.pk'; //valid

$9xyz = 'Hello world'; //invalid; starts with a number

$_xyz = 'Hello world'; //valid; starts with an underscore

$_9xyz = 'Hello world'; //valid

$aäa = 'Hello world'; //valid; 'ä' is (Extended) ASCII 228.

?>

The End

Page-8

You might also like