Unit - Iv
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
● 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:
<html>
A PHP script starts with <?php and ends
with ?>: <body>
<?php <?php
?> ?>
</body>
</html>
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.
1. <?php Output:
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
<!DOCTYPE html>
<html>
<body>
<?php
// This is 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
● 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.
● 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 { ?>
<?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.
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) {
}
<!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;
</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>
</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:
Radio Buttons
The gender fields are radio buttons and the HTML code looks like this:
Gender:
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.
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