0% found this document useful (0 votes)
14 views22 pages

PHP Mysql LM - Exp Number - 4

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

PHP Mysql LM - Exp Number - 4

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

G.S.

Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

LIST OF EXPERIMENTS
1. Program based on HTML tags (text formatting, images, font, hyperlink, list and

tables)

2. Program based on CSS (Inline, Internal and External CSS) , css properties for

position, text, font, borders, margin, padding, color, shadow, images.

3. Program based on HTML form elements using Bootstrap.

4. Program based on PHP variables, Decisions and loops.

5. Program based on different types of Arrays in PHP.

6. Program based on Form Validation through regular expression.(phone number and

email validations)

7. Program based on Functions (Simple and Parameterized Functions, Function call by

values and references, Optional parameter, Return statement, Recursion)

8. Demonstration on Database and table, relationships using MYSql.

9. Program based on Database connectivity though PHP and MYSql.

10. Program based on Insert, Select, Update and Delete operations on database tables.

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 1|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Experiment Number: 04
Title of the exercise : Program based on PHP variables, Decisions and loops.

About PHP
 PHP stands for PHP: Hypertext Preprocessor
 PHP is a server-side scripting language
 PHP scripts are executed on the server
 PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.)
 PHP is an open source software
 PHP is free to download and use

Basic PHP Syntax


 A PHP script starts with <?php and ends with ?>
 The default file extension for PHP files is ".php"
 A PHP file normally contains HTML tags, and some PHP scripting code
 PHP statements are terminated by semicolon (;)
 In PHP, all user-defined functions, classes, and keywords (e.g. if, else,
while, echo, etc.) are not case-sensitive

Hello World example


<html>
<body>
<?php
// Use echo to print on console
echo ‚Hello World!‛;
?>
</body>
</html>

Go to htdocs folder which is present in the apache2triad installed folder. There


create a folder and save this program with .php extension such as Hello.php.
To execute hello world program, type in the address bar as follows:
http://localhost/MyPHPProgram/hello.php

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 2|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Comments in PHP
 // Single line comment (C++ and Java-style comment)
 # Single line comment (Shell-style comments)
 /* Multiple line comment
(C-style comments) */

PHP Variable
 In PHP, a variable does not need to be declared before adding a value to it
 PHP automatically converts the variable to the correct data type,
depending on its value
 In PHP, the variable is declared automatically when you use it
 PHP variables must begin with a “$” sign
 Variables are used for storing values, like text strings, numbers or arrays
 The correct way of declaring a variable in PHP:

$var_name = value;

PHP Variables Example


<html>
<body>
<?php
$a = 25; // Numerical variable
$b = ‚Hello‛; // String variable
$c = 5.7; // Float variable
echo ‚Number is : ‛.$a.‚<br/>‛;
echo ‚String is : ‛.$b.‚<br/>‛;
echo ‚Float value : ‛.$c;
?>
</body>
<html>
OUTPUT of the above given Example is as follows:

Number is : 25

String is : Hello

Float value : 5.7

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 3|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Global and locally-scoped variables


 Global variables can be used anywhere
 Local variables restricted to a function or class

Example for Global and locally-scoped variables

<html>
<body>
<?php
$x=24; // global scope

// Function definition
function myFunction() {
$y=59; // local scope
echo "Variable x is: $x <br>";
echo "Variable y is: $y";
}

myFunction();// Function call

echo "Variable x is: $x";


echo "<br>";
echo "Variable y is: $y";
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

Variable x is:

Variable y is: 59

Test variables outside the function:

Variable x is: 24
Variable y is:

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 4|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Static Keyword in PHP


 Static keyword is used when you first declare the variable
 Each time the function is called, that variable will still have the information
it contained from the last time the function was called

Static Keyword Example


<html>
<body>
<?php
// Function definition
function myFunction() {
static $x=45;
echo $x;
echo "<br/>";
$x++;
}
// Function call
myFunction();
myFunction();
myFunction();
myFunction();
myFunction();
?>
<body>
<html>

OUTPUT of the above given Example is as follows:

45
46
47
48
49

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 5|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

ECHO and PRINT statements in PHP


 ECHO - It can output one or more strings
 PRINT – It can only output one string, and returns always 1
 ECHO is faster compared to PRINT as echo does not return any value
 ECHO is not a function and, as such, it does not have a return value
 If you need to output data through a function, you can use PRINT() instead:

Example:

echo 50;
print (50);
PRINT Statement Example in PHP
<html>
<body>
<?php
// Use ‘print’ to print on console
print "Hello world!<br>***********";
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

Hello world!
***********

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 6|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Constant in PHP
 define() function is used to set a constant
 It takes three parameters they are:
1. Name of the constant
2. Value of the constant
3. Third parameter is optional. It specifies whether the constant name
should be case-insensitive. Default is false

Constant string Example


<html>
<body>
<?php
/* Here constant name is ‘Hai’ and ‘Hello
Friend’ is its constant value and true
indicates the constant value is case-
insensitive */
define("Hai","Hello Friend",true);
echo hai;
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

Hello Friend

PHP Example to calculate the area of the circle


<html>
<body>
<?php
// defining constant value PI = 3.14
define("PI","3.14");
$radius=15;
$area=PI*$radius*$radius;
echo "Area=".$area;
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

Area=706.5

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 7|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Operators in PHP
Arithmetic Operators
 Arithmetic operators allow performing basic mathematical operations

Operator Description Example Result

+ Addition $a = 2 + 5; $a=7

- Subtraction $a = 10 - 2; $a=8

* Multiplication $a = 2 * 5; $a=10

/ Division $a = 15 / 5; $a=3

% Modulus $a = 23 % 7; $a=3.28

$a =5; $a=6
++ Increment
$a ++;

$a =5; $a=4
-- Decrement
$a --;

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 8|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Arithmetic Operators Example


<html>
<body>
<?php
// Add 20, 10 and sum is stored in $i
$i=(20 + 10);
// Subtract $i, 5 and difference is stored in $j
$j=($i - 5);
// Multiply $j, 4 and result is stored in $k
$k=($j * 4);
// Divide $k, 2 and result is stored in $l
$l=($k / 2);
// Devide $l, 5 and remainder is stored in $m
$m=($l % 5);
echo "i = ".$i."<br/>";
echo "j = ".$j."<br/>";
echo "k = ".$k."<br/>";
echo "l = ".$l."<br/>";
echo "m = ".$m."<br/>";
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

i = 30
j = 25
k = 100
l = 50
m=0

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 9|P age


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Increment and Decrement Operators

Operator Name Description

++$a Pre-increment Increments $a by one, then returns $a

$a++ Post-increment Returns $a, then increments $a by one

--$a Pre-decrement Decrements $a by one, then returns $a

$a-- Post-decrement Returns $a, then decrements $a by one

Increment and Decrement Operators Example


<html>
<body>
<?php
$i=10;
$j=20;
$i++;
$j++;
echo $i."<br/>";
echo $j."<br/>";
// Post increment
$k=$i++;
// Pre increment
$l=++$j;
echo $k."<br/>";
echo $l;
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

11
21
11
22

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 10 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Assignment Operators in PHP


 Assignment operator is used to write a value to a variable

Operator Example Is the same as

= 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

%= x%=y x=x%y

Assignment Operators Example


<html>
<body>
<?php
$a=5;
echo "a=".$a;
echo "<br/>";

$b=10;
$b += 20;
echo "b=".$b;
echo "<br/>";
$c=15;
$c -= 5;
echo "c=".$c;
echo "<br/>";
$d=20;
$d *= 2;
echo "d=".$d;
echo "<br/>";

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 11 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

$e=25;
$e /= 5;
echo "e=".$e;
echo "<br/>";
$f=30;
$f %= 4;
echo "f=".$f;
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

a=5
b=30
c=10
d=40
e=5
f=2

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 12 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

String Operators in PHP

Operator Name Example Result

$a = "Hello"
. Concatenation $b = "Hello world!"
$b = $a . " world!"

Concatenation $a = "Hello"
.= $a = "Hello world!"
Assignment $a .= " world!"

String Operators Example

<html>
<body>
<?php
$a = "Hello";
$b = $a . " Friend!";
echo $b;
echo "<br/>";

$c="Good";
$c .= " Day!";
echo $c
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

Hello Friend! Good Day!

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 13 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Conditional Statements in PHP :

The if Statement in PHP


 If statement executes some code only if a specified condition is true.

Syntax:

if (condition) {
code to be executed if condition is true;
}

The if Statement Example


<html>
<body>
<?php
$i=0;

/* If condition is true, statement is


executed*/
if($i==0)
echo "i is 0";
?>
<body>
</html>
if($i==0)

OUTPUT of the above given Example is as follows:


i is 0

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 14 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

The if…else Statement in PHP


 If…else statement executes some code if a condition is true and some
another code if the condition is false

Syntax:

if (condition) {
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}

The if…else Statement Example


<html>
<body>
<?php
$i=1;

/* If condition is true, statement1 is


executed, else statement2 is executed*/
if($i==0)
echo "i is 0"; //statement1
else
echo "i is not 0"; //statement2
?>
<body>
</html>

OUTPUT of the above given Example is

i is not 0

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 15 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

The if…elseif…else Statement in PHP


 If…elseif…else statement selects one of several blocks of code to be
executed

Syntax:

if (condition) {
code to be executed if condition is true;
}

elseif (condition) {
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}

The if…elseif…else Statement Example (Comparing two numbers)


<html>
<body>
<?php
$i=22;
$j=22;
/* If condition1 is true, statement1 is executed,
if condition1 is false and condition2 is true,
statement2 is executed, if both the conditions
are false statement3 is executed */
if($i>$j)
echo "i is greater"; //statement1
elseif($i<$j)
echo "j is greater"; //statement2
else
echo "numbers are equal"; //Statement3
?>
<body>
</html>

OUTPUT of the above given Example is as follows:

numbers are equal

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 16 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Switch Statement in PHP


 Switch statement selects one from multiple blocks of code to be executed

Syntax:

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;
}
Switch Statement Example
<html>
<body>
<?php
$x=3;
/* Expression value is compared with each case
value. If it matches, statements following
case would be executed. Break statement is
used to terminate the execution of
statement.*/
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
?>
}
</body>
</html> OUTPUT of the above given Example is as follows:

Number 3

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 17 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Loop Control Statements in PHP.

For loop in PHP


 PHP for loop executes a block of code, a specified number of times

Syntax:

for (initialization; test condition; increment/decrement) {


code to be executed;
}

For loop Example

<html>
<body>
<?php

echo "Numbers from 1 to 20 are: <br>";

/*in for loop, initialization usually declares


a loop variable, condition is a Boolean
expression such that if the condition is true,
loop body will be executed and after each
iteration of loop body, expression is executed
which usually increase or decrease loop
variable*/

for ($x=0; $x<=20; $x++) {


echo "$x ";
}
?>
</body>
</html>

OUTPUT of the above given Example is as

follows: Numbers from 1 to 20 are:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 18 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Declaring multiple variables in for loop Example


<html>
<body>
<?php

/* Multiple variables can be declared in


declaration block of for loop */

for ($x=0,$y=1,$z=2;$x<=3;$x++) {
echo "x = $x, y = $y, z = $z <br>";
}
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

x = 0, y = 1, z = 2
x = 1, y = 1, z = 2
x = 2, y = 1, z = 2
x = 3, y = 1, z =2

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 19 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

While Loop in PHP


 While loop, loops through a block of code as long as the specified
condition is true

Syntax:

while (condition) {
code to be executed;
}

While Loop Example


<html>
<body>
<?php
$i=1;

/* here <condition> is a Boolean expression.


Loop body is executed as long as condition is
true*/

while($i<5){
echo "i is = $i <br>";
$i++;
}
?>
</body>
</html>

OUTPUT of the above given Example is as follows:

i is = 1
i is = 2
i is = 3
i is = 4

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 20 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

Do While loop in PHP


 Do while loop will always execute the block of code once, it will then
check the condition, and if the condition is true then it repeats the
loop

Syntax:

do {
code to be executed;
} while (condition );

Do While loop Example


<html>
<body>
<?php
$i=1;

/* here <condition> is a Boolean expression. Please


note that the condition is evaluated after executing
the loop body. So loop will be executed at least
once even if the condition is false*/

do
{
echo "i is = $i <br>";
$i++;
}while($i<5);
?>
</body>
</html>

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 21 | P a g e


G.S.Mandal’s,
Marathwada Institute of Technology, Aurangabad
Department of Computer Applications
PRACTICAL - LAB MANAUL
CLASS : FY-MCA- 2021-22 PART-I SUBJECT : PHP MySql Lab

OUTPUT of the above given Example is as


follows:

i is =
1 i is
= 2 i
is = 3
i is =
4

MIT/FYMCA/LM/PHPMySql/2021-22/04 Prepared By : Dr. Chintal. P. L 22 | P a g e

You might also like