0% found this document useful (0 votes)
29 views39 pages

Unit - Iv

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

Unit - Iv

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

UNIT - IV

Introduction to PHP
Introduction to PHP
PHP is Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page.

It is the server-side scripting language embedded with HTML to develop Static website, Dynamic website, or Web
applications.

PHP used to design the dynamic web applications with MySQL database.

The websites like www.facebook.com, www.yahoo.com are also built on PHP. It can be easily embedded with HTML
files and HTML codes can also be written in a PHP file.

The thing that differentiates PHP and HTML is, PHP codes are executed on the server whereas HTML codes are
directly rendered on the browser.
PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the
server-side. PHP is well suited for web development. Therefore, it is used to develop web applications

Why use PHP

● It handles dynamic content, database as well as session tracking for the website.
● You can create sessions in PHP.
● It can access cookies variable and also set cookies.
● It helps to encrypt the data and apply validation.
● PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
● Using PHP language, you can control the user to access some pages of your website.
● As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.
● PHP can handle the forms, such as - collect the data from users using forms, save it into the database, and
return useful information to the user. For example - Registration form.
PHP Features
PHP is very popular language because of its simplicity and open source. There are some important features of PHP
given below:
Web Development
PHP is widely used in web development nowadays. PHP can develop dynamic websites easily. But
you must have the basic the knowledge of following technologies for web development as well.

○ HTML
○ CSS
○ JavaScript
○ Ajax
○ XML and JSON
○ jQuery
Install PHP
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all
operating systems. There are many AMP options available in the market that are given below:

● WAMP for Windows


● LAMP for Linux
● MAMP for Mac
● SAMP for Solaris
● FAMP for FreeBSD
● XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components
too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.
Basic PHP Syntax <!DOCTYPE html>

<html>
A PHP script starts with <?php and ends
with ?>: <body>

<h1>My first PHP page in IWT class</h1>

<?php <?php

// PHP code goes here echo "Hello World!";

?> ?>

</body>

</html>

Note: PHP statements end with a


semicolon (;).
PHP Echo

PHP echo is a language construct, not a function. Therefore, you don't need to
use parentheses with it. But if you want to use more than one parameter, it is
required to use parenthesis.

The syntax of PHP echo is given below:

1. void echo ( string $arg1 [, string $... ] )


PHP echo statement can be used to print the string, multi-line strings, escaping characters,
variable, array, etc. Some important points that you must know about the echo statement are:

● echo is a statement, which is used to display the output.


● echo can be used with or without parentheses: echo(), and echo.
● echo does not return any value.
● We can pass multiple strings separated by a comma (,) in echo.
● echo is faster than the print statement.

1. <?php Output:

2. echo "Hello by PHP echo"; Hello by PHP echo


3. ?>
PHP Case Sensitivity
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-
sensitive.

<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body> Note: However; all variable names are case-sensitive!


</html>
Comments in PHP

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment


?>

</body>
</html>
Creating (Declaring) PHP Variables
<!DOCTYPE html>
<html>
<body>
In PHP, a variable starts
with the $ sign, followed by <?php
the name of the variable: $txt = "Hello world!";
$x = 5;
$y = 10.5;

echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>

</body>
</html>
Primitive data types refers to the type of data that can be held in a variable or constant.
They are called primitive because the data type is fundamental to the language

PHP supports the following data types:

● String
● Integer
● Float (floating point numbers - also called double)
● Boolean
● Array
● Object
● NULL
● Resource
PHP Integer PHP Boolean
PHP String Rules for integers: A Boolean represents two possible
<?php states: TRUE or FALSE.
● An integer must have at least
$x = "Hello world!";
one digit
$y = 'Hello world!'; ● An integer must not have a
decimal point $x = true;
● An integer can be either
echo $x; positive or negative $y = false;
● Integers can be specified in:
echo "<br>";
decimal (base 10), hexadecimal
echo $y; (base 16), octal (base 8), or
binary (base 2) notation
?>
<?php

$x = 5985;

var_dump($x);

?>
PHP Array
An array stores multiple values in one single variable.

<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Operators
Operators are used to perform operations on variables
and values.

PHP divides the operators in the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Increment/Decrement operators
● Logical operators
● String operators
● Array operators
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>

<?php <?php
echo "<h2>PHP is Fun!</h2>"; print "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>"; print "Hello world!<br>";
echo "I'm about to learn PHP!<br>"; print "I'm about to learn PHP!";
echo "This ", "string ", "was ", "made ", "with ?>
multiple parameters.";
?> </body>
</html>
</body>
</html>
PHP Control Statement

All the conditional statements and loop statements are collectively termed as Control Statements.
Conditional Statements:
Conditional statements are used in a code to perform an action only if a particular condition is satisfied.
PHP provides four types of conditional statements. These are:
<!DOCTYPE html>
if: <html>
<body>
If statement is used to perform an action if a single condition is true.
<?php
$t = date("H");
Syntax
if ($t < "20") {
if (condition) { echo "Have a good day!";
}
code to be executed if condition is true; ?>

} </body>
</html>
..else: <!DOCTYPE html>
If else statement is used to <html>
perform an action if a single <body>
condition is true and to perform
another action if that condition <?php
$t = date("H");
is false.
Syntax if ($t > "20") {
echo "Have a good day!";
if (condition) {
} else {
code to be executed if echo "Have a good night!";
condition is true;
}
} else { ?>
code to be executed if
condition is false; </body>
</html>
}
..elseif….else: <!DOCTYPE html>
<html>
If elseif else statement is used to <body>
perform different actions for
<?php
different conditions.Syntax $t = date("H");
echo "<p>The hour (of the server) is " . $t;
if (condition) { echo ", and will give the following message:</p>";
code to be executed if this
condition is true; if ($t < "10") {
echo "Have a good morning!";
} elseif (condition) { } elseif ($t < "20") {
echo "Have a good day!";
code to be executed if
} else {
first condition is false and
echo "Have a good night!";
this condition is true;
}
} else { ?>

code to be executed if all


conditions are false;
</body>
}
</html>
Syntax
switch:
switch (n) {
Switch statement is used to
case label1:
select one of the action to
code to be executed if n=label1;
perform from multiple actions,
break;
relative to the true condition.
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
<!DOCTYPE html>
<html>
<body>

<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

</body>
</html>
Loop Statements:

Loop Statements are used to execute a block of code continuously as long as the condition of the loop
is true, and stops only when the condition fails.

PHP provides three types of loop statements. These are:

For loop:

For loop is used to execute a group of action only for a specified number of times.

Syntax
for (init counter; test counter; increment counter) {

code to be executed for each iteration;

}
<!DOCTYPE html>
<html>
<body>

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

</body>
</html>
While Loop:
<!DOCTYPE html>
While loop is used to execute a group of <html>
action as long as the specified condition is <body>
true.
<?php
Syntax $x = 1;

while (condition is true) { while($x <= 5) {


echo "The number is: $x
code to be executed; <br>";
}
$x++;
}
?>

</body>
</html>
Do While Loop:
<!DOCTYPE html>
Do While loop is used to execute a block of
<html>
code at least once and then to repeat the <body>
actions as long as the specified condition is
true. <?php
$x = 6;

do {
Syntax echo "The number is: $x <br>";
do { $x++;
} while ($x <= 5);
code to be executed; ?>
} while (condition is true);
</body>
</html>
PHP Form Handling
<!DOCTYPE HTML>
<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
Text Fields
The name, email, and website fields are text input elements, and the comment field is
a textarea. The HTML code looks like this:

Name: <input type="text" name="name">

E-mail: <input type="text" name="email">

Website: <input type="text" name="website">

Comment: <textarea name="comment" rows="5" cols="40"></textarea>

Radio Buttons
The gender fields are radio buttons and the HTML code looks like this:

Gender:

<input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="male">Male

<input type="radio" name="gender" value="other">Other


Cookies
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the
user's computer. 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.

Create Cookies With PHP


A cookie is created with the setcookie() function.

Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
A cookie in PHP is a small file with a maximum size of 4KB that the web server stores on
the client computer. They are typically used to keep track of information such as a
username that the site can retrieve to personalize the page when the user visits the website
next time. A cookie can only be read from the domain that it has been issued from. Cookies
are usually set in an HTTP header but JavaScript can also set a cookie directly on a
browser.
Setting Cookie In PHP: To set a cookie in PHP, the setcookie() function is used. The
setcookie() function needs to be called prior to any output generated by the script otherwise
the cookie will not be set.
Syntax:
setcookie(name, value, expire, path, domain, security);
PHP Sessions
A session is a way to store information (in variables) to be used across multiple pages.

Unlike a cookie, the information is not stored on the users computer.

When you work with an application, you open it, do some changes, and then you close it. This is much like a
Session. The computer knows who you are. It knows when you start the application and when you end. But
on the internet there is one problem: the web server does not know who you are or what you do, because the
HTTP address doesn't maintain state.

Session variables solve this problem by storing user information to be used across multiple pages (e.g.
username, favorite color, etc). By default, session variables last until the user closes the browser.

So; Session variables hold information about one single user, and are available to all pages in one application.
END

You might also like