WT Unit 5
WT Unit 5
Unit 5
By
Asst. Prof. N. D. Dhamale MET IOE ,Nashik
M.E. Computer
PHP: Introduction to PHP, uses of PHP, general syntactic characteristics,
functions, pattern matching, form handling, files, cookies, session tracking, using
2. PHP is compatible with almost all servers used today (Apache, IIS, etc.)
A PHP file normally contains HTML tags, and some PHP scripting code.
PHP Variables
Variables are "containers" for storing information.
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
$x = 5;
$y = "John";
In the example above, the variable $x will hold the value 5, and the variable $y will
hold the value "John"
Output : PHP echo and print Statements
1. In PHP, output refers to the way you display data to the user
1. echo
2. print
2. echo and print are more or less the same. They are both used to output data to
the screen.
<html>
<body>
<?php
echo "Hello";
//same as:
echo("Hello");
?>
</body>
</html>
echo
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
Q. Explain PHP with basic code block for displaying Hello world
A simple .php file with both HTML code and PHP code:
<html>
<body>
<h1>My first PHP page</h1>
<?php echo "Hello World!"; ?>
</body>
</html>
HTML Structure:
1. The code starts with the <html> tag, indicating the beginning of an HTML document.
2. Inside the <html> tag, there’s a <body> tag, which contains all the content that will be
displayed in the browser.
PHP Code:
1. The <? php ... ?> tags denote the beginning and end of a PHP code block. The server
processes this code before sending the HTML to the browser.
2. echo is a PHP construct that outputs the specified string directly to the browser.
<html>
<body>
<h1>My first PHP page</h1>
<?php echo "Hello World!"; ?>
</body>
</html>
Output
Display Variables using echo
<html>
<body>
<?php
echo "<h2>$txt1</h2>";
echo "<p>Study PHP at $txt2</p>";
?>
</body>
</html>
Output
PHP print Statement
The print statement can be used with or without parentheses: print or print()
<!DOCTYPE html>
<html>
<body>
<?php
print "Hello";
//same as:
print("Hello");
?>
</body>
</html>
Primitives [Data Type ]
primitive data types are the basic building blocks for data manipulation. PHP
supports several primitive types, which can be classified as follows:
Conditional Statements
a. if Statement
b. if...else Statement
c. if...elseif...else Statement
d. switch Statement
2. Looping Statements
a. for Loop
b. while Loop
c. do...while Loop
d. foreach Loop
Q. Write a program to explain control statements in Php
Example :
<html>
<body>
<?php
$age=20;
if ($age >= 18)
{
echo "You are an adult.";
}
?>
Output
</body>
</html>
Q. Write a program to explain control statements in Php
b. if...else Statement
Executes one block of code if the condition is true and another block if it’s false.
<html>
<body>
<?php
$age=20;
<html>
<body>
<?php
$score=77;
Exmpale
<html>
<body>
<?php
$day="Friday";
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
case "Friday":
echo "End of the workweek!";
break;
default:
echo "Midweek days.";
Output
}
?>
</body>
</html>
Looping Statements
a. for Loop
Used to execute a block of code a specific number of times.
b. while Loop
Executes a block of code as long as the specified condition is true.
$i = 0;
while ($i < 5)
{
echo $i;
$i++;
}
Looping Statements
c. do...while Loop
Similar to the while loop, but it guarantees that the block of code will execute at least once.
$i = 0;
do
{
echo $i;
$i++;
}
while ($i < 5);
d. foreach Loop
Specifically designed for iterating over arrays.
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[0];
?>
</body>
</html> Output
Volvo
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
<html>
<body>
<?php
?>
</body>
</html>
Output
Mustang
PHP Multidimensional Arrays
A multidimensional array is essentially an array that contains one or more arrays as
its elements. $cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
<html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
?>
</body>
</html>
PHP User Defined Functions
1. A function is a block of statements that can be used repeatedly in a program.
2. A function will not execute automatically when a page loads.
3. A function will be executed by a call to the function.
Example
function myMessage()
{
echo "Hello world!";
}
PHP User Defined Functions
Call a Function
<!DOCTYPE html>
<html>
<body>
<?php
function myMessage() {
echo "Hello world!";
}
myMessage();
?>
</body>
</html>
PHP Form Handling
The <form> tag is used to create an HTML form for user input.
The PHP superglobals $_GET and $_POST are used to collect form-data.
GET :
1. Information sent from a form with the GET method is visible to everyone (all variable
names and values are displayed in the URL)
2. GET also has limits on the amount of information to send .
3. The limitation is about 2000 characters
4. GET may be used for sending non-sensitive data.
POST :
1. Information sent from a form with the POST method is invisible to others (all
names/values are embedded within the body of the HTTP request) and has no limits on
the amount of information to send.
2. Moreover POST supports advanced functionality such as support for multi-part binary
input while uploading files to server.
<form> action Attribute
action
The action attribute specifies where to send the form-data when a form is submitted.
Eg.
<html>
<body>
<form action="welcome.php" method="post">
User Name: <input type="text" name="name"><br>
Password : <input type="password" name="pass"><br>
<input type="submit">
</form>
</body>
</html>
welcome.php
<html>
<body>
User name <?php echo $_POST["name"]; ?><br>
password <?php echo $_POST["pass"]; ?>
</body>
</html>
PHP Cookies
1. A cookie is often used to identify a user.
2. A cookie is a small file that the server embeds on the user's computer.
3. Each time the same computer requests a page with a browser, it will send the cookie too.
With PHP, you can both create and retrieve cookie values.
1. The following example creates a cookie named "user" with the value “Ntiin ".
2. The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie is
available in entire website (otherwise, select the directory you prefer).
3. We then retrieve the value of the cookie "user" (using the global variable $_COOKIE).
4. We also use the isset() function to find out if the cookie is set
PHP Cookies
<?php
$cookie_name = "Nitin";
$cookie_value = "AI and DS Dept.";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
}
else
{
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
PHP File Handling
File handling is an important part of any web application. You often need to open
and process a file for different tasks.
PHP has several functions for creating, reading, uploading, and editing files.
<html>
<body>
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread ($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
</body>
</html>
Example Write Operation
<!DOCTYPE html>
<html>
<body>
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "AI & DS Branch \n";
fwrite($myfile, $txt);
fclose($myfile);
?>
</body>
</html>
PHP Sessions
1. A session is a way to store information (in variables) to be used across multiple
pages
2. Unlike a cookie, the information is not stored on the users computer.
3. A session is started with the session_start() function.
4. Session variables are set with the PHP global variable: $_SESSION.
5. The session_start() function must be the very first thing in your document.
Before any HTML tags.
let's create a new page called "demo_session1.php". In this page, we start a new PHP
session and set some session variables:
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
MySQL with PHP
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
WAP and WML
1. WAP [Wireless application protocol]
2. WAP is a protocol that is introduced in 1999, which stands for Wireless application
protocol.
3. It offers Internet communications over wireless devices, such as mobile phones.
4. Most of the wireless networks are supported by WAP.
5. It enables access to the internet in mobile devices and uses the mark-up language like
WML, which stands for Wireless Markup Language that is referred to as XML 1.0
application.
6. A WAP gateway is a server.
WAP Model
1. In the mobile device, the user opens the web browser and access the website and visit
webpages accordingly.
2. The mobile device forwards the URL request to a WAP gateway through the network
using the WAP protocol.
3. Then, the WAP gateway refers to this request over the internet after translating it into a
conventional HTTP URL request.
4. The specified Web server accepts the request and processes the request. Then, it returns
the response to the mobile device in the WML file through the WAP gateway that will be
displayed in the web browser on the device.
WML [Wireless Markup Language ]
1. WML stands for Wireless Markup Language (WML) which is based on HTML
and HDML.
2. It is specified as an XML document type. It is a markup language used to
develop websites for mobile phones.
3. While designing with WML, constraints of wireless devices such as small
display screens, limited memory, low bandwidth of transmission and small
resources have to be considered.
4. WML is used only for WAP sites on mobile phones and can be hosted only be
WAP hosts that support WML
5. WML sites are monochrome.
6. The concept WML follows is that of a deck and card metaphor.
7. A WML document is thought of as made up of many cards.
8. WAP site has many cards. One card will be displayed at a time on the screen,
just like how one page is displayed at a time in an HTML website.
WML [Wireless Markup Language ]
Many cards can be inserted into a WML document, and the WML deck is identified by a
URL. To access the deck, the user can navigate using the WML browser, which fetches the
deck as required
<wml>
</wml>
Overview of the .NET Framework
productive and versatile platform for developing a wide range of applications, from
Programmatically, when our program needs memory, CLR allocates the memory for scope
and de-allocates the memory if the scope is completed.
Language Compilers (e.g. C#, VB.Net, J#) will convert the Code/Program to Microsoft
Intermediate Language (MSIL) intern this will be converted to Native Code by CLR.
2. .Net Framework Class Library (FCL)
This is also called as Base Class Library and it is common for all types of applications i.e.
the way you access the Library Classes and Methods in VB.NET will be the same in C#, and
it is common for all other languages in .NET.
The following are different types of applications that can make use of .net class library.
1. Windows Application.
2. Console Application
3. Web Application.
4. XML Web Services.
5. Windows Services.
It describes set of data types that can be used in different .Net languages in common. (i.e),
CTS ensures that objects written in different .Net languages can interact with each other.
It is a sub set of CTS and it specifies a set of rules that needs to be adhered or satisfied by all
language compilers targeting CLR. It helps in cross language inheritance and cross language
debugging.
Overview of C#
1. C# (pronounced "C-sharp") is a modern, object-oriented programming language
developed by Microsoft as part of the .NET Framework.
2. It was designed to be simple, powerful, and flexible, with strong support for software
development across a wide range of platforms—from web and mobile applications to
game development and enterprise-level systems.
Key Features of C#
Object-Oriented Programming (OOP)
1. Encapsulation: C# supports data encapsulation, which allows classes to hide their
internal workings and expose only necessary functionality via public methods and
properties.
2. Inheritance: C# supports inheritance, allowing classes to derive functionality
from other classes and promote code reuse.
3. Polymorphism: C# enables polymorphism through method overriding and
interface implementation, allowing different classes to implement the same
interface in different ways.
4. Abstraction: C# allows for abstract classes and interfaces that enable abstraction,
ensuring that certain implementation details are hidden from the user.
Introduction to ASP.NET
1. ASP.NET is a web application framework designed and developed by Microsoft.
ASP.NET is open source and a subset of the .NET Framework and successor of the
classic ASP(Active Server Pages). With version 1.0 of the .NET Framework, it was first
released in January 2002.
2. ASP.NET is built on the CLR(Common Language Runtime) which allows the
programmers to execute its code using any .NET language(C#, VB etc.).
3. It is specially designed to work with HTTP and for web developers to create dynamic
web pages, web applications, web sites, and web services as it provides a good
integration of HTML, CSS, and JavaScript.
4. .NET Framework is used to create a variety of applications and services like Console,
Web, and Windows, etc. But ASP.NET is only used to create web applications and web
services. That’s why we termed ASP.NET as a subset of the .NET Framework.
ASP.NET Controls
ASP.NET provides web forms controls that are used to create HTML components
It is used to create a
Button Click, Command
button.
It is used to create an
ImageButton Click imagesButton. Here, an
image works as a Button.
It is used to create a hyperlink
Hyperlink None control that responds to a click
event.
It is used to create a dropdown list
DropDownList SelectedIndexChanged
control.
It is used to create a ListBox
ListBox SelectedIndexCnhaged
control like the HTML control.
CancelCommand, EditCommand,
DeleteCommand, ItemCommand, It used to create a frid that is used
SelectedIndexChanged, to show data. We can also perform
DataGrid
PageIndexChanged, paging, sorting, and formatting
SortCommand, UpdateCommand, very easily with this control.
ItemCreated, ItemDataBound
CancelCommand, EditCommand,
DeleteCommand, ItemCommand,
It is used to create datalist that is
DataList SelectedIndexChanged,
non-tabular and used to show data.
UpdateCommand, ItemCreated,
ItemDataBound
CheckBox CheckChanged It is used to create checkbox.