PHP and Mysql Unit-1
PHP and Mysql Unit-1
PHP and Mysql Unit-1
Tech /BCA
UNIT-I
The Building blocks of PHP
Introduction:
PHP files can contain text, HTML, CSS, JavaScript, and PHP code , PHP code
are executed on the server, and the result is returned to the browser as plain HTML , PHP
files have extension ".php".
Advantages of PHP :
Open source: It is developed and maintained by a large group of PHP developers, this will
helps in creating a support community, abundant extension library.
Easy to use: It uses C like syntax, so for those who are familiar with C, it's very easy for them
to pick up and it is very easy to create website scripts.
Stable: Since it is maintained by many developers, so when bugs are found, it can be quickly
fixed.
Powerful library support: we can easily find functional modules you need such as PDF,
Graph etc.
Built-in database connection modules: You can connect to database easily using PHP, since
many websites are data/content driven, so we will use database frequently, this will largely
reduce the development time of web apps.
Can be run on many platforms, including Windows, Linux and Mac, it's easy for users to find
hosting service providers.
WWW.GVRJOBS4U.COM Page 1 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
PHP is a server side scripting language that is embedded in HTML. It is integrated with a
number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and
Microsoft SQL Server.
<html>
<body>
<?php
// Use echo to print on console
echo “Hello World!”;
?>
</body>
</html>
Variables in PHP
A variable is a special container that we can define, which then “holds” a value, such as a
number, string, object, array, or a Boolean. A variable can have a short name (like x and y) or
a more descriptive name (age, carname, total_volume).
• A variable starts with the $ sign, followed by the name of the variable
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different variables)
Examples : <?php
$x = 5;
$y = 4;
echo $x + $y;
?>
In PHP, variables can be declared anywhere in the script. The scope of a variable is the part
of the script where the variable can be referenced/used.
WWW.GVRJOBS4U.COM Page 2 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
• local
• Global
• Static
Global Scope : A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
Example <?php
$x = 5; // global scope
function myTest() {
myTest();
?>
Local Scope : A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function
Example
<?php
function myTest()
{ $x = 5; // local scope
} myTest();
?>
Static :-
Normally, when a function is completed/executed, all of its variables are deleted. However,
sometimes we want a local variable NOT to be deleted. We need it for a further job.
WWW.GVRJOBS4U.COM Page 3 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
To do this, use the static keyword when you first declare the variable:
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
. $_GET contains any variables provided to a script through the GET method. .
$_POST contains any variables provided to a script through the POST method. .
$_SERVER contains information such as headers, file paths, and script locations.
$_ENV contains any variables provided to a script as part of the server environment. .
$_REQUEST contains any variables provided to a script via GET, POST, or COOKIE input
mechanisms.
:Data Types:
The Properties of data is called datatype . PHP has a total of eight data types which we use
to construct our variables
Simple Types:
WWW.GVRJOBS4U.COM Page 4 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Compound Types:
• Resources − are special variables that hold references to resources external to PHP
(such as database connections).
Integers:-
Ex: $int_var=12345;
Float or Double:
A float (floating point number) is a number with a decimal point or a number in exponential
form.
Ex: $many=2.8855;
Boolean:
A string is a sequence of characters, like "Hello world!". A string can be any text inside
quotes. You can use single or double quotes.
WWW.GVRJOBS4U.COM Page 5 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Example: $x = null;
Operators In PHP
Operators are symbols that tell the PHP processor to perform certain actions. For example,
the addition (+) symbol is an operator that tells PHP to add two variables or values, while
the greater-than (>) symbol is an operator that tells PHP to compare two values.
The following lists describe the different operators used in PHP.
Arithmetic Operators
The arithmetic operators are used to perform common arithmetical operations, such as
addition, subtraction, multiplication etc. Here's a complete list of PHP's arithmetic
operators:
Operator Description Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y.
* Multiplication $x * $y Product of $x and $y.
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
Assignment Operators
Comparison Operators:
The comparison operators are used to compare two values in a Boolean fashion.
Operator Name Example Result
WWW.GVRJOBS4U.COM Page 6 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Logical Operators:
String Operators:
There are two operators which are specifically designed for strings.
Operator Description Example Result
. Concatenation $str1 . $str2 Concatenation of $str1 and $str2
.= Concatenation assignment $str1 .= $str2 Appends the $str2 to the $str1
Array Operators:
WWW.GVRJOBS4U.COM Page 7 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Constant in PHP:
• A constant is a name or an identifier for a fixed value. Constant are like variables
except that once they are defined, they cannot be undefined or changed.
• Constants are very useful for storing data that doesn't change while the script is
running.
• Common examples of such data include configuration settings such as database
username and password, website's base URL, company name, etc.
• Constants are defined using PHP's define() function, which accepts two arguments:
the name of the constant, and its value.
• define(name, value, case-insensitive)
• name: Specifies the name of the constant
• value: Specifies the value of the constant
• case-insensitive: Specifies whether the constant name should be case-
insensitive. Default is false
• Once defined the constant value can be accessed at any time just by referring to its
name.
Example:
<?php
// Defining constant
define("SITE_URL", "https://www.freejobs4us.com/");
// Using constant
echo 'Thank you for visiting - ' . SITE_URL;
?>
Like most programming languages, PHP also allows you to write code that perform different
actions based on the results of a logical or comparative test conditions at run time.
WWW.GVRJOBS4U.COM Page 8 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
There are several statements in PHP that you can use to make decisions:
• The if statement
• The if...else statement
• The if...elseif....else statement
• The switch...case statement
The if Statement:
The if statement is used to execute a block of code only if the specified condition evaluates
to true. This is the simplest PHP's conditional statements and can be written like:
if(condition){
// Code to be executed
}
Example:
<?php
$age = 18;
if($age>=18 ){
echo “You are Major – U R Eligible for Vote !";
} ?>
WWW.GVRJOBS4U.COM Page 9 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
?>
Switch…Case:
The switch-case statement is an alternative to the if-elseif-else statement, which does
almost the same thing. The switch-case statement tests a variable against a series of values
until it finds a match, and then executes the block of code corresponding to that match.
switch(n)
{
case label1:
// Code to be executed if n=label1
break;
case label2:
// Code to be executed if n=label2
break;
...
default:
// Code to be executed if n is different from all labels
}
WWW.GVRJOBS4U.COM Page 10 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Example:
<?php
$today = date("D");
switch($today){
case "Mon":
echo "Today is Monday. Clean your house.";
break;
case "Tue":
echo "Today is Tuesday. Buy some food.";
break;
case "Wed":
echo "Today is Wednesday. Visit a doctor.";
break;
case "Thu":
echo "Today is Thursday. Repair your car.";
break;
case "Fri":
echo "Today is Friday. Party tonight.";
break;
case "Sat":
echo "Today is Saturday. Its movie time.";
break;
case "Sun":
echo "Today is Sunday. Do some rest.";
break;
default:
echo "No information available for that day.";
break;
}
?>
PHP Loops
Different Types of Loops in PHP
Loops are used to execute the same block of code again and again, until a certain condition
is met. The basic idea behind a loop is to automate the repetitive tasks within a program to
save the time and effort. PHP supports four different types of loops.
• while — loops through a block of code until the condition is evaluate to true.
• do…while — the block of code executed once and then condition is evaluated. If the
condition is true the statement is repeated as long as the specified condition is true.
• for — loops through a block of code until the counter reaches a specified number.
WWW.GVRJOBS4U.COM Page 11 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
{
// Code to be executed
}
Example:
<?php
$i = 1;
while($i <= 3){
$i++;
echo "The number is " . $i . "<br>";
}?>
PHP do…while Loop:
The do-while loop is a variant of while loop, which evaluates the condition at the end of
each loop iteration. With a do-while loop the block of code executed once, and then the
condition is evaluated, if the condition is true, the statement is repeated as long as the
specified condition evaluated to is true.
do{
// Code to be executed
}
while(condition);
Example:
<?php
$i = 1;
do{
$i++;
echo "The number is " . $i . "<br>";
}
while($i <= 3);
?>
Difference Between while and do…while Loop:
The while loop differs from the do-while loop in one important way — with a while loop, the
condition to be evaluated is tested at the beginning of each loop iteration, so if the
conditional expression evaluates to false, the loop will never be executed.
With a do-while loop, on the other hand, the loop will always be executed once, even if the
conditional expression is false, because the condition is evaluated at the end of the loop
iteration rather than the beginning.
WWW.GVRJOBS4U.COM Page 12 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
Example:
<?php
for($i=1; $i<=10; $i=i+1){
echo "The number is " . $i . "<br>";
}
?>
PHP Functions
A function is a self-contained block of code that can be called by your scripts. When A
function is called, the function’s code is executed and performs a particular task. You can
pass values to a function, which then uses the values appropriately—storing them,
transforming them, displaying them, whatever the function is told to do. When finished, a
function can also pass a value back to the original code that called it into action.
WWW.GVRJOBS4U.COM Page 13 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
The declaration of a user-defined function start with the word function, followed by the
name of the function you want to create followed by parentheses i.e. () and finally place
your function's code between curly brackets {}.
Example:
<?php
function test()
{
echo "Welcome to My First Function ";
}
test();
?>
add(2, 3);
?>
Example:
<?php
WWW.GVRJOBS4U.COM Page 14 of 15
Web Applications Development using PHP& MYSQL B.Sc./ B.Tech /BCA
WWW.GVRJOBS4U.COM Page 15 of 15