AWD - Unit-1 Introduction To PHP
AWD - Unit-1 Introduction To PHP
Semester - 5
Prebuilt content is same every time the page is Content is generated quickly and changes
loaded. regularly.
It uses the server-side languages such
It uses the HTML code for developing a website. as PHP, Servlet, JSP, and ASP.NET etc. for
developing a website.
It sends exactly the same response for every It may generate different HTML for each of
request. the request.
The content is only changed when someone The page contains server-side code which
publishes and updates the file (sends it to the web allows the server to generate the unique
server). content when the page is loaded.
Ease of Use: PHP is easy to learn. Syntax of PHP is somewhat like C language. A lot of Ready-
made PHP scripts are freely available in market so, you can use them in your project or get
some help from them.
HTML Support: PHP can be embedded within HTML. PHP allows us to interact with HTML
page dynamically. We can also embed HTML code within PHP script.
Database Support: PHP supports many databases like MySQL, Oracle, Sybase, Generic ODBC
etc. We can create database in MySQL and connect it with PHP script to perform database
operations like insert, update or delete.
Cross-platform Compatibility: PHP and MySQL run on every popular flavor of Unix and
windows. A huge percentage of the world’s HTTP servers run on one of these two categories of
operating system.
PHP is compatible with the three leading Web Servers
o Apache HTTP Server for Unix and Windows
o MS Internet Information Server(IIS)
o Netscape Enterprise Server.
Stability: The word stable means two different things in this context:
o The server doesn’t need to be rebooted often
o PHP doesn’t change drastically and incompatibly from release to release.
write and test programs running from your local computer exactly the same way they will be
seen on internet.
Apache is the most popular open source web server which runs on Windows, Linux and many
other OS.
The scripts written in PHP can be run by Apache which can contain text, images, MP3 or
graphics data.
Apache is stable and easy to use once it is installed.
Apache also supports a huge range of modules like GD Library and other for handling security.
Q – 6 Explain the relationship between Apache, MySQL and PHP (AMP module).
AMP stands for Apache web server, MySQL database and PHP language.
All these three are open source. They don’t need to be purchased from market. They are freely
available on internet.
PHP is used to create web pages. MySQL is used to create database for the storage of website
data and Apache is used to run PHP pages.
The combination of these there allows us to create dynamic interactive websites. It can be
installed on Windows (WAMP), Linux platform (LAMP) or XAMPP for both Windows and Linux
platforms.
Once you install the PHP and Apache server, you should start the Apache service and then type
following in web browser.
http://10.9.1.35/……. OR http://localhost/webpage.php
Where 10.9.1.35 is the IP of local computer. Localhost is your local server means the Apache
web server installed in your computer.
Q – 8 Explain the general structure and syntax of PHP with example. How does PHP
work?
Any PHP file generally may contain some HTML tags, CSS, JavaScript and PHP code.
You can use any PHP editor like Notepad or Dreamweaver to write PHP code. The file must be
saved with .php extension. If the file is saved with .html extension then the php code will not be
executed.
There are different PHP tags which can be used to write PHP script (code). They are followings.
1. Standard PHP Tag
<?php
echo "Hello Friends";
?>
2. Short Tag
<?
echo "Hello Friends";
?>
Generally the standard tag is used in routine PHP programming.
Two statements are used to display the text on PHP page: echo and print.
PHP is server side scripting language that means script code is processed on the web server like
Apache or IIS rather than on client side.
Semicolon: Each code line must ends with semicolon which is the separator used to separate
one instruction from next instruction.
White Space: White space is ignored between PHP statements.
Case Sensitivity: PHP is case sensitive scripting language.
Comments: In PHP // is used for single line comment and /* */ is used for multi-line comment.
Special Characters can be displayed on screen with backslash.
Web browser is software which acts as mediator between client side code and server side code.
We can use any web browser like Internet Explorer, Mozilla Firefox or Google Chrome. When
we type the URL in the web browser, web browser sends a request to the web server. Then web
server runs the PHP script on that page. PHP module executes that script and sends the result
to the web browser which we can see on the screen.
Code which is written within <?php…..?> tag is processed by web server. Code which is written
outside<?php…..?> tag is processed by web browser itself.
Example:
<html>
<head><title>Welcome to PHP Programming</title></head>
<body>
<h1><b>This is my first PHP program</b></h1>
<?php
echo "Hello Friends!!!!! PHP is very interesting language";
?>
</body></html>
When we run the web page which contains both HTML and PHP tags in web browser, the web
browser only sends PHP code to web server which sends results to the web browser in return.
The HTML code is parsed and executed by the web browser itself and we get the final output.
Example:
<html>
<title> This is my first PHP code </title>
<body>
<?php
echo "Welcome in PHP world";
?>
</body>
</html>
Example: <?php
echo " <b> One</b><br>";
echo " <i> Two </i><br> ";
echo " <u> Three </u><br> ";
?>
Output: One
Two
Three
Q – 10 What is variable? Give rules for naming the variables with example.
Variable holds any type of data in PHP code. It can hold number, string, Boolean, objects or
resources.
In PHP variable begins with “$” sign and “=” sign is used to assign value to variable. “$” sign is
not the part of variable name but it is used to recognize that the string or character starts with $
is variable for PHP parser.
In PHP we don’t need to declare the type of variable because it automatically takes the type of
assigned value. The value of variable may be changed at any time in code. Type casting is not
required in PHP code because it performs type casting automatically according to the assigned
data. So PHP is also known as Loosely Typed Language.
Rules for Variable Names
Variable must start with letter or underscore(_).
Digits from 0-9 are allowed in between the variable characters but variable name must not
start with digit.
Space is not allowed within the variable name.
We can’t user reserved words as variable names.
Example:
<?php
$a = 10;
$num = 'friend';
$_sum = 25.60;
$ab5 = "Happy Diwali";
$b = ture;
?>
Boolean Boolean has only two possible values: true or false $x = true;
$y = false;
NULL NULL means nothing not even 0. $x = null;
String String is sequence of characters. Single quoted $x = "Hello world!";
strings are treated as string constants while double $y = 'Hello world!';
quoted strings replace variable with their values.
Array Array is a collection of values. $color=array("Red","Blue","
Green");
Object Object is an instance of user defined class which is class Car
used to access the variables and functions of that {
class. function Car()
{
$this->color = "Red";
}
}
$c1 = new Car();
echo $c1->color;
Resource Resource is a special type of variable that holds -
references to resources external to PHP such as
database connections.
Example :
<?php
echo ("Hello World");
echo "This is Printing"."This is working";
echo "This is intersting","programming";
$a=10;
$b=20;
echo $a, $b;
?>
print
print is used to print one or more strings or variable value.
print() is not actually a function (it is a language construct), so you are not required to use
parentheses with it.
You can’t print multiple values with comma operator but you can concate multiple values with
dot (.) operator.
Print function returns integer value. The returned value is 1.
Syntax: int print ( arg )
Example:
<?php
print "Hello World";
print("Hello World");
$a=10;
$b=20;
$c = print $a;
print $c;
print $a.$b;
?>
You can print multiple values with comma You can’t print multiple values with comma
operator. You can also concate multiple values operator but you can concate multiple values
with dot (.) operator. with dot (.) operator.
$a = 10;
$b = 'Hello';
$c = 56.23;
$d = true;
echo gettype($a); //prints integer
$a = "bar"; // string
$b = true; //Boolean
echo gettype($a); //string
echo gettype($b); // boolean
settype($a,"integer");
settype($b, "string");
echo gettype($a); //integer
echo gettype($b); //string
?>
You can use global keyword to declare any variable global directly when you want to use global
variable within the function.
Example:
<?php
$ x=10; //global variable
$ y=20; //global variable
function getvalue()
{
global $x , $y;
$x = $x +5;
$y = $y +5;
}
getvalue();
echo $x."<br>".$y;
?>
Output: x = 15
y =25
As shown in above example variables x and y are global variables. These global variables can’t
be directly used within any function. As shown above if you want to get value of x and y within
the getvalue() then you have to declare them global again in function. If you will not do this step
then it will display the error like undefined variable x and undefined variable y. But if you
declare x and y as global in function then you will get x = 15 and y = 25 in output.
In PHP, $GLOBALS is special associative super global array which holds all global variables
defined in the script. Name of the global variable is the key and the content of the global
variable is the value of the array element.
Accessing Global Variable from GLOBALS Array
Example:
<?php
function getvalue()
{
static $x = 0;
$x = $x + 5;
echo $x;
}
getvalue();
getvalue();
?>
Output: 5 10
Static variable is initialized only once when the function is called first time and its value is
preserved even after function ends. As shown in above example x is static so outside the
getvalue() function we will get incremented value of x. If x is not declared as static then outside
the function we will get x=5 each time function is called.
Static variables are generally used in recursive functions.
Example:
<?php
const a=10;
define("ename","Atmiya");
echo ename."<br>";
echo constant("ename")."<br>";
echo a;
?>
Output: Atmiya
Atmiya
10
PHP provides some in-built constants which can be used in any PHP script.
Assignment Operators
= $x=$y $x=$y
+= $x += $y $x = $x + $y
-= $x -= $y $x= $x - $y
*= $x *= $y $x=$x * $y
/= $x /= $y $x=$x / $y
%= $x%=$y $x=$x % $y
Comparison Operators
Logical Operators
$n = 11;
?>
String Operators
There are two string operators.
The first is the concatenation operator ('.'), which returns the concatenation of its right and left
arguments.
The second is the concatenating assignment operator ('.='), which appends the argument on the
right side to the argument on the left side.
Example:
<?php
++
$a = "Hello ";
$b = $a. "World!"; // now $b contains "HelloWorld!"
echo $b;
$a = "Hello ";
$a.="World!"; // now $a contains "HelloWorld!"
echo $a;
?>
Switch statement
If Statement
The if construct is one of the most important features of many languages, PHP included. It allows for
conditional execution of code fragments. PHP features an if structure that is similar to that of C:
Syntax:
if (expression)
{
Statements;
}
expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute
statements, and if it evaluates to FALSE – it will ignore it.
Example:
<?php
$a =15;
$b =10;
if ($a > $b)
{
Prepared By: Bhumika S. Zalavadia Page 19
Subject Name and Code: Advance Web Development (21DCECC503) Unit-1
}
?>
}
else
{
echo "$a is smaller than $b";
}
?>
$x = 2;
switch ($x)
{
case1:
echo "Number 1";
break;
case2:
echo "Number 2";
break;
case3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
Output: Number2
{
statements
}
The meaning of a while statement is simple. It tells PHP to execute the nested statements
repeatedly, as long as the while expression evaluates to TRUE.
The value of the expression is checked each time at the beginning of the loop, so even if this
value changes during the execution of the nested statements, execution will not stop until the
end of the iteration
Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested
statements won't even be run once.
Example:
<?php
$i = 1;
while ($i <= 5)
{
echo $i++;
}
?>
Output: 1 2 3 45
Do…While Loop
In Do…while loop expression is checked at the end of each iteration instead of in the beginning.
The main difference from regular while loop is that do…while is executed at least once because
condition is checked at the end of loop.
Syntax:
do
{
statements
} while (expr);
Example:
<?php
$i = 5;
do
{
echo $i;
$i--;
} while ($i > 0);
?>
Output: 5 4 3 21
For Loop
For loop is generally used when we know that how many time we want to iterate the loop.
Syntax:
for (initialization ; condition ; increment)
{
statements
}
Initialization is the starting value of looping variable. Condition must be evaluated true to run
the loop. Increment is the number by which looping variable is incremented.
Example: Display numbers from 1 to 10
<?php
for ($i = 1; $i <= 10; $i++)
{
echo $i;
}
?>
Output: 1 2 3 4 5 6 7 8 9 10
Foreach Loop
This loop is used to iterate over an array elements. On each loop, the value of the current element is
assigned $value and the array pointer is advanced by one - so on the next loop, you'll be looking at the
next element.
Syntax:
foreach (array as value)
{
code to be executed;
}
Example: Print the values of the given array
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br>";
}
?>
Output: Value: one
Value: two
Value: three
Break Statement
Break statement ends execution of the current for, foreach, while, do-while or switch structure. Break
accepts an optional numeric argument which tells it how many nested enclosing structures are to be
broken out of.
Example:
<?php
for($i=0 ; $i<=10 ; $i++)
{
if($i = = 3)
break;
echo $i;
}
?>
Output: 0 1 2
Continue Statement
Continue is used within looping structures to skip the rest of the current loop iteration and
continue execution at the condition evaluation and then the beginning of the next iteration.
Example:
<?php
for ($i = 0; $i < 5; $i++)
{
if ($i == 2)
continue;
echo$i;
}
?>
Output: 0 1 3 4
*********************************