0% found this document useful (0 votes)
28 views6 pages

Hsslive CS Chapt 10 Server Side PHP

Uploaded by

adhilsherin69
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)
28 views6 pages

Hsslive CS Chapt 10 Server Side PHP

Uploaded by

adhilsherin69
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/ 6

Computer Science – XII Chapter 10

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.

Output statements – echo() and print()

vardump(): To display both data type and value of variables.

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.

Data Types in PHP


(i) Core data types – Integer, Float/Double, String, Boolean
(ii) Special data types – Null, Array, Object, Resource

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.);

Eg: $marks = array(54, 45, 56, 60, 40);


$days = array(“sun”, “mon”, “tue”, “wed”, “thu”, “fri”, “sat”);
echo $marks[0]; gives 54 as output.
echo $days[3]; gives wed as output.

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.);

Eg: $marks = array(“hari”=>54, “ravi”=>45, “mini”=>56);


echo $marks[“hari”]; gives 54 as output.

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.

Three tier architecture in PHP

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

Form Methods – Get and Post

Connecting PHP to Database (MySQL)


Connection to a MySQL database is done through the following steps:
1. Open a connection to MySQL.  mysql_connect()
2. Specify the database we want to open.  mysql_select_db()
3. Retrieve data from or insert data into database.  mysql_query()
mysql_fetch_array()
4. Close the connection.  mysql_close()

Questions from Previous Years’ Question Papers (Computer Science)


1. PHP is
(a) freeware (b) proprietary (c) both (d) none (1) (March 2016)
2. (a) Compare Indexed and Associative arrays in PHP. (2) (March 2016)
(b) Write a PHP program to display prime numbers below 50. (3) (March 2016)
(c) Write a PHP program to display the perfect numbers below 100. (3) (March 2016)
3. An array in PHP is given as follows:
$family = array(“Father”=>”Ajay”, “Son”=>”Arjun”, “Daughter” =>”Archana”).
Name the type of this array. Write a foreach loop that prints all the array elements.
(2) (SAY 2016)
4. (a) What are the differences between GET and POST methods in form submitting?
(2) (SAY 2016)
(b) Study the following steps and determine the correct order:
(1) Open a connection to MySQL server
(2) Execute the SQL query
(3) Fetch the data from query.
(4) Select database
(5) Close connection
(a) 4, 1, 2, 3, 5 (b) 1, 4, 2, 3, 5
(c) 1, 5, 4, 2, 3 (d) 4, 1, 3, 2, 5 (1) (SAY 2016)
5. A web server that supports PHP on any operating system is ____. (1) (March 2017)

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)

Answers of some questions:


7. GET and POST are the values used with Method attribute of <FORM> tag. The skeleton of
the HTML form with these methods will be as follows:
<FORM Action= "data.php" Method= "get">
.............
</FORM>

<FORM Action= "data.php" Method= "post">


.............
</FORM>
When we use GET method, the data remains visible in the address bar since contents
are passed as part of the URL. GET can send only 2000 characters.
But if we use POST method, the data is not visible as contents are passed to the script as
an input file. There is no limit in the size of data.

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

You might also like