Web Programming Ktu Notes
Web Programming Ktu Notes
CHAPTER NO : 8
CHAPTER NAME : Server Side Programming
with PHP
LECTURE NO: 2
1
PHP Logical Operators
The PHP logical operators are used to combine conditional
statements.
Example
<?php
$a = 10;
$b = 4;
$c = 15;
echo $a>$b and $b<$c;
?>
PHP String Operators
PHP has two operators that are specially designed for strings.
Example
Example: -
<?php
$t = 21;
if ($t < "20") {
echo "Have a good day!";
}
?>
The if...else Statement
The if...else statement executes some code if a condition is
true and another code if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
<?php
$t = 10;
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The if...elseif...else Statement
The if...elseif...else statement executes different codes for
more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
Example
<?php
$t = 9;
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
do...while Loop
The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The PHP for Loop
The for loop is used when you know in advance how
many times the script should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP Arrays
An array stores multiple values in one single variable:
Create an Array in PHP
In PHP, the array() function is used to create an array.
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Indexed arrays
In PHP, the array() function is used to create an array.
Arrays with a numeric index.
<?php
$fruits = array('apple','orange','grapes','mango');
echo $fruits[0];
?>
Empty Array
$fruits = array();
count()
function used to get the length of the array.
<?php
$fruits = array('apple','orange','grapes','mango');
echo count($fruits);
?>
Iterating Arrays
for loop can be used to run through the array elements.
<?php
$fruits = array('apple','orange','grapes','mango');
for($i=0;$i<count($fruits);$i++)
{
echo $fruits[$i] . '<br>';
}
?>
Iterate the PHP Arrays using foreach
The foreach loop - Loops through a block of code for each element in
an array.
<?php
$fruits = array('apple','orange','grapes','mango');
foreach($fruits as $value)
{
echo $value . '<br>';
}
?>
foreach() with key and value
<?php
$fruits = array('apple','orange','grapes','mango');
foreach($fruits as $key=>$value){
echo $key." ". $value . '<br>';
}
?>
Array Operators
+ operator can be used perform the union operation between
two arrays.
The Union operator appends the right-hand array appended to left-
hand array. ;
If a key exists in both arrays, the elements from the left-hand array
will be used, and the matching elements from the right-hand array
will be ignored.
<?php
$x = Array(1,2,3,4);
$y = Array(5,6,7,8,9,10);
$z = $x + $y;
var_dump($z);
?>
Comparison of arrays
== operator used to compare two arrays
<?php
$x = Array(1,2,3,4);
$y = Array(1,2,3,4);
echo ($x==$y);
?>
PHP Associative Arrays
Associative arrays are arrays that use named keys that
you assign to them.
<?php
$names = array();
//save data
$names['101'] = 'john';
$names['102'] = 'anish';
$names['103'] = 'victor';
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
server.php
<?php
$name = $_GET['fname'];
echo $name;
?>
Example
server.php
<?php
$name = $_POST['fname'];
echo $name;
?>
How HTTP submits form data?
http://localhost/PhpProject1/register.php?user=arul&pass=cse&email=arul%40gmail.com
How HTTP submits form data?
http://localhost/PhpProject1/register.php?user=arul&pass=c
se&email=arul%40gmail.com
PHP Program
HTTP writes the form data in Super global variable $_GET automatically
GET vs. POST
Both GET and POST create an array (e.g. array( key1 =>
value1, key2 => value2, key3 => value3, ...)).
This array holds key/value pairs, where keys are the names of
the form controls and values are the input data from the user.
$_GET is an array of variables passed to the current script via the
URL parameters.
$_POST is an array of variables passed to the current script via the
HTTP POST method.