0% found this document useful (0 votes)
18 views13 pages

Theory Part Prgm1,2

Jdnd

Uploaded by

vihitog478
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)
18 views13 pages

Theory Part Prgm1,2

Jdnd

Uploaded by

vihitog478
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/ 13

PHP PROGRAMMING LABORATORY BAI358D

INTRODUCTION TO HTML
Web site: A set of interconnected web pages, usually including a homepage, generally
located on the same server, and prepared and maintained as a collection of information by a
person, group, or organization.
Web Page: A web page is a document that's created in html that shows up on the internet
when you type in or go to the web page's address.
Types of Web Pages:

• Static web page: is delivered exactly as stored, as web content in the web server's file
system. Contents cannot be changed.

• Dynamic web page: is generated by a web application that is driven by server-side


software or client-side scripting. Dynamic web pages help the browser (the client) to enhance
the web page through user input to the server. Contents can be changed as evolution over
time.
Client –Server Model

The client–server model is a distributed application structure in computing that partitions


tasks or workloads between the providers of a resource or service, called servers, and service
requesters, called clients. Often clients and servers communicate over a computer network. A
server is a host that is running one or more server programs which share their resources with
clients. A client requests a server's content or
service function.

Figure 1: Client-Server Model


Web –Server

Web server refers to either the hardware (the computer) or the software (the computer
application) that helps to deliver web content that can be accessed through the Internet.
The most common use of web servers is to host websites, but there are other uses such as
gaming, data storage or running enterprise application.

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 1


PHP PROGRAMMING LABORATORY BAI358D

Figure 2: Web-Server

General structure of a Web Page


A basic HTML page contains a Head section and a Body section. The contents of the head
section are normally invisible in a web browser and mainly consists of some Metatags.
The Body consist of those HTML elements that you want to have displayed in your browser.
<html>
<head>
</head>
<body>
</body>
</html>
Scripting language: A scripting language or script language is a programming language
that supports the writing of scripts, programs written for a special runtime environment that
can interpret and automate the execution of tasks which could alternatively be executed one
by-one by a human operator.
URL: A uniform resource locator (URL), also known as web address, is a specific character
string that constitutes a reference to a resource. In most web browsers, the URL of a web
page is displayed on top inside an address bar. An example of a typical URL would be
"http://en.example.org/wiki/Main_Page".

ILLUSTRATING HTML TAGS AND THEIR ATTRIBUTES


HTML: Hyper Text Markup Language is the most widely used language to write web
pages.it is a markup language.
Hypertext : Refers to the way in which web pages are linked together.
Markup Language: The user simply markups a text document with tags that tell a web
browser how to structure it to display.
Creating HTML document : To begin coding HTML user needs only two things:
1. A simple text editor (notepad).
2. A web browser.

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 2


PHP PROGRAMMING LABORATORY BAI358D

Simple steps to create a basic HTML document:


1. Open notepad or another text editor.
2. At the top of the page type <html>
3. Add the opening header tag <head>
4. On the next line type <title> give title for page </title>
5. Go to next line and type closing header tag </head>
6. Go to next line and type opening body tag<body>
7. Go to next line and type closing body tag</body>
8. Finally, go to next line and type</html>
9. In the file menu, choose save as.
10. In the save as type option box, choose all files.
11. Name the file filename.html
12. Click save.
HTML document structure: An HTML document starts and ends with <html> and
</html> tags. These tags tell the browser that the entire document is composed in
HTML. Inside these two tags, the document is split into 2 sections:
1. The <head>……</head> elements contains information about the document such as title
of the document etc.
2. The <body>…. </body> elements contains the real content of the document that you see
on your screen.
ATTRIBUTES:
An attribute is used to define the characteristics of an element and is placed inside the
element’s opening tag. All attributes are made up of 2 parts: a name and a value.
-The name is the property you want to set.
-The value is what you want the value of the property to be.
Example: <font face=”arial” color=”red”>

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 3


PHP PROGRAMMING LABORATORY BAI358D

INTRODUCTION TO PHP
PHP (recursive acronym for PHP: Hypertext Pre-processor) is a widely-used open source
general-purpose scripting language that is especially suited for web development and can be
embedded into HTML. It allows web developers to create dynamic content that interacts with
databases. PHP is basically used for developing web based software applications.
PHP is mainly focused on server-side scripting, so you can do anything any other CGI
program can do, such as collect form data, generate dynamic page content, or send and
receive cookies. Code is executed in servers, that is why you’ll have to install a sever-like
environment enabled by programs like XAMPP which is an Apache distribution.

Where is PHP used?


Three are the main areas where PHP scripts are used:
• Server-side scripting
This is the most used and main target for PHP. You need three things to make this work the
way you need it. The PHP parser (CGI or server module), a web server and a web browser.
You need to run the web server where. You can access the PHP program output with a web
browser, viewing the PHP page through the server. All these can run on your home machine
if you are just experimenting with PHP programming.
• Command line scripting
You can make a PHP script to run it without any server or browser. You only need the PHP
parser to use it this way. This type of usage is ideal for scripts regularly executed using cron
(on Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text
processing tasks.
• Writing desktop applications
PHP may not the very best language to create a desktop application with a graphical user
interface, but if you know PHP very well, and would like to use some advanced PHP features
in your client-side applications you can also use PHP-GTK to write such programs. You also
have the ability to write cross-platform applications this way. In this article, we’ll have a
detailed look at the server-side scripting using PHP.

Why PHP?
There stand convincing arguments for all those who wonder why PHP is so popular today:
• Compatible with almost all servers used nowadays
A web server is an information technology that processes requests via HTTP, the basic
network protocol used to distribute information on the World Wide Web. There exist many
types of web servers that servers use. Some of the most important and well-known are:
Apache HTTP Server, IIS (Internet Information Services), lighted, Sun Java System Web
Server etc. As a matter of fact, PHP is compatible with all these web servers and many more.
• PHP will run on most platforms
Unlike some technologies that require a specific operating system or are built specifically for
that, PHP is engineered to run on various platforms like Windows, Mac OSX, Linux, Unix
etc)

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 4


PHP PROGRAMMING LABORATORY BAI358D

• PHP supports such a wide range of databases


An important reason why PHP is so used today is also related to the various databases it
supports (is compatible with). Some of these databases are: DB++, dBase, Ingres, Mongo,
MaxDB, MongoDB, mSQL, Mssql, MySQL, OCI8, PostgreSQL, SQLite, SQLite3 and so
on.
• PHP is free to download and open source
Anyone can start using PHP right now by downloading it from php.net. Millions of people
are using PHP to create dynamic content and database-related applications that make for
outstanding web systems. PHP is also open source, which means the original source code is
made freely available and may be redistributed and modified.
• Easy to learn & large community
PHP is a simple language to learn step by step. This makes it easier for people to get engaged
in exploring it. It also has such a huge community online that is constantly willing to help you
whenever you’re stuck (which actually happens quite a lot). The graphic below shows a basic
workflow of dynamic content being passed to and from the client using PHP:

Figure 3: PHP Dynamic Content Interaction


XAMPP Setup
XAMPP is a free and open source cross-platform web server solution developed by Apache
Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters
for scripts written in the PHP and Perl programming languages. In order to make your PHP
code execute locally, first install XAMPP.
• Download XAMPP
• Install the program (check the technologies you want during installation)
• Open XAMPP and click on "Start" on Apache and MySQL (when working with databases)

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 5


PHP PROGRAMMING LABORATORY BAI358D

Figure 4: XAMPP window after a successful installation with Apache and MySQL enabled

Place your web project inside the htdocs directory. In the common case, if you installed
XAMPP directly inside the C: drive of your PC, the path to this folder would be:
C:xampphtdocs

Figure 5: XAMPP Directory for Web Projects


To test the services are up and running you can just enter localhost in your address bar and
expect the welcoming page.

PHP Language Basics


The aim of this section is to introduce the general syntax you should expect in PHP.
• Escaping to PHP
There are four ways the PHP parser engine can differentiate PHP code in a webpage:

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 6


PHP PROGRAMMING LABORATORY BAI358D

• Canonical PHP Tags


This is the most popular and effective PHP tag style and looks like this:
<?php...?>
• Short-open Tags
These are the shortest option, but they might need a bit of configuration, and you
might either choose the --enable-shorttags configuration option when building PHP,
or set the short_open_tag setting in your php.ini file.
<?...?>
• ASP-like Tags
In order to use ASP-like tags, you’ll need to set the configuration option in the php.ini
file:
<%...%>
• HTML script Tags
You can define a new script with an attribute language like so:
<script language="PHP">...</script>
ii. Commenting PHP
Just like other languages, there are several ways to comment PHP code. Let’s have a
look at the most useful ones:
Use # to write single-line comments
<?
# this is a comment in PHP, a single line comment
?>
Use // to also write single-line comments
<?
// this is also a comment in PHP, a single line comment
?>
Use /* ...*/ to write multi-line comments
<?
/* this is a multiple line comment
Name: Web Code
Type: Website
Purpose: Web Development
*/
?>
PHP Data Types
• Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 7


PHP PROGRAMMING LABORATORY BAI358D

PHP VARIABLE
• Variables are "containers" for storing information.
• Creating (Declaring) PHP Variables.
• In PHP, a variable starts with the $ sign, followed by the name of the variable.
• A variable name must start with a letter or the underscore character.
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric characters and underscores (Az, 0-9,
and _ ).
• Variable names are case-sensitive ($age and $AGE are two different variables).
Output Variables
• The PHP echo and print statement is often used to output data to the screen.
• echo has no return value while print has a return value of 1 so it can be used
in expressions. echo can take multiple parameters (although such usage is rare)
while print can take one argument. echo is marginally faster than print.

PHP Function
A user-defined function declaration starts with the word function:
Syntax
function functionName() {
code to be executed;
}
Function names are NOT case-sensitive.

• Function Arguments:
✓ Information can be passed to functions through arguments. An argument is
just like a variable.
✓ Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.
• Functions - Returning values
✓ To let a function return a value, use the return statement.
PHP File Handling

➢ The fopen() function is used to an open file. The first parameter of fopen() contains
the name of the file to be opened and the second parameter specifies in which mode
the file should be opened.
➢ The fclose() function is used to close an open file.
➢ The feof() function checks if the "end-of-file" (EOF) has been reached. The feof()
function is useful for looping through data of unknown length.
➢ The fwrite() function is used to write to a file. The first parameter of fwrite() contains
the name of the file to write to and the second parameter is the string to be written

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 8


PHP PROGRAMMING LABORATORY BAI358D

PROGRAMS
1 a. Develop a PHP program to calculate areas of Triangle and Rectangle.

<?php
function calculateTriangleArea($base, $height) {
return 0.5 * $base * $height;

}
function calculateRectangleArea($length, $width) {

return $length * $width;


}

$triangleBase = 6;
$triangleHeight = 8;

$rectangleLength = 5;
$rectangleWidth = 10;

$triangleArea = calculateTriangleArea($triangleBase, $triangleHeight);


$rectangleArea = calculateRectangleArea($rectangleLength, $rectangleWidth);

echo "Triangle Area: " . $triangleArea . " square units<br>";


echo "Rectangle Area: " . $rectangleArea . " square units<br>";

?>
Output

Triangle Area: 24 square units


Rectangle Area: 50 square units

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 9


PHP PROGRAMMING LABORATORY BAI358D

1 b. Develop a PHP program to calculate Compound Interest.


<?php

function calculateCompoundInterest($principal, $rate, $time, $compoundsPerYear)


{

$compoundInterest = $principal * pow(1+ ($rate/ $compoundsPerYear),


$compoundsPerYear * $time) - $principal;

return $compoundInterest;
}

$principal = 1000;
$rate = 0.05;

$time = 2;
$compoundsPerYear = 12;

$compoundInterest=calculateCompoundInterest($principal,$rate,$time,$compoundsPerYear)
;

echo "Principal Amount: $" . $principal . "<br>";


echo "Annual Interest Rate: " . ($rate * 100) . "%<br>";

echo "Time (in years): " . $time . "<br>";


echo "Compounds Per Year: " . $compoundsPerYear . "<br>";

echo "Compound Interest: $" . round($compoundInterest, 2) . "<br>";


echo "Total Amount: $" . round($principal + $compoundInterest, 2);
?>

Output
Principal Amount: $1000
Annual Interest Rate: 5%
Time (in years): 2
Compounds Per Year: 12
Compound Interest: $104.94
Total Amount: $1104.94

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 10


PHP PROGRAMMING LABORATORY BAI358D

2. Develop program(s) to demonstrate concatenation of strings:


(i) Strings represented with literals (single quote or double quote)

<?php
$string1 = 'Hello,';

$string2 = 'world!';
$concatenatedSingle = $string1 . $string2;

$string3 = "This is ";


$string4 = "a concatenated string.";

$concatenatedDouble = $string3 . $string4;


echo "Using Single Quotes: " . $concatenatedSingle . "\n";

echo "Using Double Quotes: " . $concatenatedDouble . "\n";


?>

Output
Using Single Quotes: Hello, world!

Using Double Quotes: This is a concatenated string.

(ii) Strings as variable


<?php

$string1 = "Hello, ";


$string2 = "world!";
$concatenatedString = $string1 . $string2;

echo $concatenatedString;
?>

Output
Hello, world!

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 11


PHP PROGRAMMING LABORATORY BAI358D

(iii) Multiple strings represented with literals (single quote or double quote) and
variables

<?php
$name = "John";

$age = 25;
$string1 = 'My name is ' . $name . ' and I am ' . $age . ' years old.';

$string2 = "My name is $name and I am $age years old.";


echo "String 1: $string1 <br>";

echo "String 2: $string2 <br>";


?>

Output
String 1: My name is John and I am 25 years old.
String 2: My name is John and I am 25 years old.

(iv) Strings and string variables containing single quotes as part string contents
<?php

$string1 = "I'm";
$string2 = "it's";

$quoteString1 = "He said, 'Hello!'";


$quoteString2 = 'She exclaimed, "Wow!"';
$concatenatedString1 = $string1 . ' great';

$concatenatedString2 = $string2 . ' amazing';


echo "Concatenated String 1: " . $concatenatedString1 . "<br>";

echo "Concatenated String 2: " . $concatenatedString2 . "<br>";


echo "String with single quotes 1: " . $quoteString1 . "<br>";

echo "String with single quotes 2: " . $quoteString2 . "<br>";


?>

Output
Concatenated String 1: I'm great
Concatenated String 2: it's amazing

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 12


PHP PROGRAMMING LABORATORY BAI358D

String with single quotes 1: He said, 'Hello!'


String with single quotes 2: She exclaimed, "Wow!"

(v) Strings containing HTML segments having elements with attributes

<?php
$element1 = '<div class="container">';

$element2 = '<h1 id="heading">Concatenation of Strings</h1>';


$element3 = '<p style="color: blue;">Strings containing HTML segments</p>';

$element4 = '</div>';
$finalHTML = $element1 . $element2 . $element3 . $element4;

echo $finalHTML;
?>

Output
Concatenation of Strings

Strings containing HTML segments

DEPT OF CSE(AIML),KLS VDIT, HAIYAL 13

You might also like