PHP Mysql LM - Exp Number - 4
PHP Mysql LM - Exp Number - 4
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
email validations)
10. Program based on Insert, Select, Update and Delete operations on database tables.
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
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;
Number is : 25
String is : Hello
<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";
}
Variable x is:
Variable y is: 59
Variable x is: 24
Variable y is:
45
46
47
48
49
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>
Hello world!
***********
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
Hello Friend
Area=706.5
Operators in PHP
Arithmetic Operators
Arithmetic operators allow performing basic mathematical operations
+ 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 --;
i = 30
j = 25
k = 100
l = 50
m=0
11
21
11
22
= 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
$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/>";
$e=25;
$e /= 5;
echo "e=".$e;
echo "<br/>";
$f=30;
$f %= 4;
echo "f=".$f;
?>
</body>
</html>
a=5
b=30
c=10
d=40
e=5
f=2
$a = "Hello"
. Concatenation $b = "Hello world!"
$b = $a . " world!"
Concatenation $a = "Hello"
.= $a = "Hello world!"
Assignment $a .= " world!"
<html>
<body>
<?php
$a = "Hello";
$b = $a . " Friend!";
echo $b;
echo "<br/>";
$c="Good";
$c .= " Day!";
echo $c
?>
</body>
</html>
Syntax:
if (condition) {
code to be executed if condition is true;
}
Syntax:
if (condition) {
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}
i is not 0
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;
}
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
Syntax:
<html>
<body>
<?php
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
for ($x=0,$y=1,$z=2;$x<=3;$x++) {
echo "x = $x, y = $y, z = $z <br>";
}
?>
</body>
</html>
x = 0, y = 1, z = 2
x = 1, y = 1, z = 2
x = 2, y = 1, z = 2
x = 3, y = 1, z =2
Syntax:
while (condition) {
code to be executed;
}
while($i<5){
echo "i is = $i <br>";
$i++;
}
?>
</body>
</html>
i is = 1
i is = 2
i is = 3
i is = 4
Syntax:
do {
code to be executed;
} while (condition );
do
{
echo "i is = $i <br>";
$i++;
}while($i<5);
?>
</body>
</html>
i is =
1 i is
= 2 i
is = 3
i is =
4