0% found this document useful (0 votes)
89 views

Web Programming Ktu Notes

S7 Ktu notes

Uploaded by

gnanjitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Web Programming Ktu Notes

S7 Ktu notes

Uploaded by

gnanjitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

COMPUTER SCIENCE

SUBJECT NAME : WEB TECHNOLOGY

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

Concatenation using comma (,) operator


PHP Array Operators
 The PHP array operators are used to compare arrays.
PHP Conditional Assignment Operators
 The PHP conditional assignment operators are used to set a
value depending on conditions:
Example
<?php
$a = 10;
$b = 4;
$result = $a>$b ? $a : $c;
echo $result;
?>
PHP Control Statements
 PHP supports the following control statements
 Conditional Statements
 Loop Statements
Conditional Statements
In PHP we have the following conditional statements:

 if statement - executes some code if one condition is true


 if...else statement - executes some code if a condition is true and
another code if that condition is false
 if...elseif...else statement - executes different codes for more than
two conditions
 switch statement - selects one of many blocks of code to be
executed
The if Statement
 The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}

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;

if ($t < "10") {


echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
The PHP switch Statement
 Use the switch statement to select one of many 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;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
In PHP, we have the following loop types:
 while - loops through a block of code as long as the specified
condition is true
 do...while - loops through a block of code once, and then repeats the
loop as long as the specified condition is true
 for - loops through a block of code a specified number of times
 foreach - loops through a block of code for each element in an array
The PHP while Loop
 The while loop executes a block of code as long as the
specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}

<?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';

//accessing using key


echo $names['102'];
?>
Loop Through an Associative Array
 To loop through and print all the values of an associative
array, you could use a foreach loop,

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Read all keys
 The array_keys() function returns an array containing the
keys.
<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
?>
Reading all values
 The array_values() function returns an array containing all the
values of an array.
<?php
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
print_r(array_values($a));
?>
PHP Global Variables – Superglobals
 Some predefined variables in PHP are "superglobals", which means
that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do
anything special.
 The PHP superglobal variables are:
 $GLOBALS
 $_SERVER
 $_POST
 $_GET
 $_REQUEST
 $_FILES
 $_COOKIE
 $_SESSION
$GLOBALS
 $GLOBALS is a PHP super global variable which is used to access global
variables from anywhere in the PHP script (also from within functions or
methods).
 PHP stores all global variables in an array called $GLOBALS[index]. The index
holds the name of the variable.
<?php
function fun(){
$y = 20;
$GLOBALS['x'] = 10;
}
fun();
echo $x . "<br>";
echo $y;
?>
$_SERVER
 $_SERVER is a PHP super global variable which holds information
about headers, paths, and script locations.

• $_SERVER['PHP_SELF'] Returns the filename of the currently executing script

• $_SERVER['SERVER_NAME'] Returns the name of the host server

• $_SERVER['HTTP_HOST'] Returns the Host header from the current request

• $_SERVER['HTTP_REFERER'] Returns the complete URL of the current page

• $_SERVER['SERVER_ADDR'] Returns the IP address of the host server

• $_SERVER['HTTP_USER_AGENT'] Returns the name of the client browser

• $_SERVER['SCRIPT_NAME'] Returns the path of the current script


$_SERVER Example
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
PHP Form Handling
 Super Global Variables
 $_GET
 To read form data send by HTTP method GET.
 $_POST
 To read form data send by HTTP method POST.
 $_REQUEST
 To read form data send by HTTP method both GET and POST.
Example

<form method="get" action=“server.php">


Name: <input type="text" name="fname"><br>
<input type="submit">
</form>

server.php

<?php
$name = $_GET['fname'];
echo $name;
?>
Example

<form method=“post" action=“server.php">


Name: <input type="text" name="fname"><br>
<input type="submit">
</form>

server.php

<?php
$name = $_POST['fname'];
echo $name;
?>
How HTTP submits form data?

HTTP GET Request

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

$_GET is an associative array

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.

You might also like