Theory Part Prgm1,2
Theory Part Prgm1,2
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.
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.
Figure 2: Web-Server
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.
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)
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
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
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) {
$triangleBase = 6;
$triangleHeight = 8;
$rectangleLength = 5;
$rectangleWidth = 10;
?>
Output
return $compoundInterest;
}
$principal = 1000;
$rate = 0.05;
$time = 2;
$compoundsPerYear = 12;
$compoundInterest=calculateCompoundInterest($principal,$rate,$time,$compoundsPerYear)
;
Output
Principal Amount: $1000
Annual Interest Rate: 5%
Time (in years): 2
Compounds Per Year: 12
Compound Interest: $104.94
Total Amount: $1104.94
<?php
$string1 = 'Hello,';
$string2 = 'world!';
$concatenatedSingle = $string1 . $string2;
Output
Using Single Quotes: Hello, world!
echo $concatenatedString;
?>
Output
Hello, world!
(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.';
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";
Output
Concatenated String 1: I'm great
Concatenated String 2: it's amazing
<?php
$element1 = '<div class="container">';
$element4 = '</div>';
$finalHTML = $element1 . $element2 . $element3 . $element4;
echo $finalHTML;
?>
Output
Concatenation of Strings