Hsslive CS Chapt 10 Server Side PHP
Hsslive CS Chapt 10 Server Side PHP
Chapter 10
Server side Scripting using PHP
PHP
PHP is one of the most popular server side scripting languages and it is an open-source
project and is completely free. It is used for creating dynamic web pages. PHP stands for
PHP Hypertext Preprocessor. PHP script produces hypertext (HTML) as a result after its
execution. PHP scripting should begin with <?php and end with ?>. The PHP code
containing text documents are saved with .php extension.
Variables in PHP
A variable in PHP starts with the $ sign, followed by the name of the variable. They are not declared.
The data type of a variable depends on the value stored in it.
$x = 56; $y = “hello”; $z = true; $a = null; are examples.
Operators
Arithmetic operators + – * / %
Increment, decrement ++ ––
Assignment operator =
Relational operators < <= > >= == !=
Logical operators || or && and ! xor
String concatenation .
Combined operators += –= *= /= %= .=
An example for String concatenation:
$x = “PHP”;
$y = “Script”;
$z = $x.$y;
The . (dot) operator will add the two strings and the variable $z will have the value
PHPScript.
1
Joy John’s CS capsules
Computer Science – XII Chapter 10
Control Statements
if (test_expression)
Statement;
if (test_expression)
statement_1;
else
statement_2;
if (test_expression1)
if statements
statement_1;
else if (test_expression2)
statement_2;
:
:
else
statement_n;
switch (variable/expression)
{
case value1: statement1; break;
switch case value2: statement2; break;
statement :
:
default: statement;
}
for (initialization; test; update)
for loop
body;
initialization;
while (test_expression)
{
while loop
body;
update;
}
initialization;
do
do – while {
loop body;
update;
} while (test_expression);
To skip the remaining statements in the loop body and
continue
continues the next iteration at the condition evaluation.
break Ends the execution of the current loop.
Arrays
There are three types of arrays: (i) Indexed arrays, (ii) Associative arrays and (iii) Multi-
dimensional arrays.
2
Joy John’s CS capsules
Computer Science – XII Chapter 10
Indexed arrays: Arrays with numeric index are called indexed arrays. They are almost
similar to arrays in C++. By default array index (or subscript) starts from zero. The function
array() can be used to create an array.
$array_name = array(value1,value2,value3,etc.);
Associative arrays: Arrays with named keys are called associative arrays. Associative arrays
have their index (or subscript) as string.
$array_name = array(key=>value, key=>value, key=>value, etc.);
foreach loop
It is used when we have an array with unknown number of elements. The foreach loop
works only on arrays and has two formats.
foreach ($array as $value)
{
//code to be executed;
}
and
foreach ($array as $key=>$value)
{
//code to be executed;
}
Eg: <?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $x)
{echo "$x "; }
?>
The output of this code will be: red green blue yellow
Built-in Functions
Function Use Syntax / Example
date() To display a date in given format date(“d-m-y”) displays a
date as 09-11-2017
chr() Returns a character from the specified ASCII chr(65) returns A
value.
strlen() Returns the length of a string. strlen(“hello”) returns 5
strpos() Finds the position of the first occurrence of strpos (“hello”, “e”)
a string inside another string. returns 1
strcmp() Compares two strings strcmp (“he”, “HE”)
returns False
3
Joy John’s CS capsules
Computer Science – XII Chapter 10
User-defined Functions
A user-defined function declaration starts with the keyword "function".
function functionName(arguments)
{
//code to be executed;
}
Arguments are optional and they are variables. Functions are invoked by function calls.
Superglobal arrays
A superglobal array is a special variable that are always available in scripts. $GLOBALS,
$SERVER, $REQUEST, $GET, $POST are some examples for superglobal arrays.
$GLOBALS is a PHP super global variable which is used to access global variables from
anywhere in the PHP script.
$_SERVER is a PHP super global variable which holds information about headers, paths,
and script locations.
$_REQUEST superglobal is an array that contains the contents of the $_GET, $_POST,
and $_COOKIE superglobals.
$_POST is widely used to collect form data after submitting an HTML form with
method="post".
$_GET is also used to collect form data after submitting an HTML form with
method="get".
4
Joy John’s CS capsules
Computer Science – XII Chapter 10
5
Joy John’s CS capsules
Computer Science – XII Chapter 10
6. Discuss about special data types used in PHP. (2) (March 2017)
7. Create an HTML form in PHP showing the difference between GET and POST method.
(3) (March 2017)
8. Write a function in PHP to find the factorial of a number. (3) (March 2017)
9. Write a PHP program to find the biggest of three numbers. (3) (SAY 2017)
10. (a) What are the differences between echo and print statements? (2) (SAY 2017)
(b) What is the difference between PHP and JavaScript? (2) (SAY 2017)
8. <? php
$n = $_GET[‘num’];
$f=1;
for ($i=1; $i<=$n; $i++)
$f = $f * $i;
echo $f;
?>
9. Assume that num1, num2, num3 are the names of the textboxes in the HTML Form submitted
to the php program.
<? php
$n1 = $_GET[‘num1’];
$n2 = $_GET[‘num2’];
$n3 = $_GET[‘num3’];
if ($n1>$n2)
$big = $n1;
else
$big = $n2;
if ($n3>$big)
$big = $n3;
echo “Biggest Number is ”.$big;
?>
6
Joy John’s CS capsules