You are on page 1of 22

FINAL EXAMINATION DECEMBER 2016

BACHELOR OF COMPUTER APPLICATION (BCA)


Final Year - Six Semesters
Web Development through Open Source Technologies
(PHP, MY SQL)

Q.1. What is PHP Scripting Language? Explain system comments using in


PHP?

Ans 1 -PHP is a server-side scripting language designed primarily for web development but also
used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994,
[4]
the PHP reference implementation is now produced by The PHP Development Team.[5] PHP
originally stood for Personal Home Page,[4] but it now stands for the recursive acronym PHP:
Hypertext Preprocessor.[6]

PHP code may be embedded into HTML or HTML5 code, or it can be used in combination with
various web template systems, web content management systems and web frameworks. PHP code
is usually processed by a PHP interpreter implemented as a module in the web server or as
a Common Gateway Interface (CGI) executable. The web server combines the results of the
interpreted and executed PHP code, which may be any type of data, including images, with the
generated web page. PHP code may also be executed with a command-line interface (CLI) and can
be used to implement standalone graphical applications.[7]

The standard PHP interpreter, powered by the Zend Engine, is free software released under
the PHP License. PHP has been widely ported and can be deployed on most web servers on almost
every operating system and platform, free of charge.[8]

The PHP language evolved without a written formal specification or standard until 2014, leaving the
canonical PHP interpreter as a de facto standard .

System comments using in PHP -This is an Ajax base Comment System and it is an
advance script where the users of your websites can easily post their comments or replies on your
updates or other information on your website using Ajax, and PHP.

The comment system uses cookie to store users information on their first time to post a comment,
reply to a posted comment or like a comment and these stored information are later saved in the
database as the login information for your users.

For a user to leave or post a comment, reply to a posted comment or like a comment, the system will
prompt the user to enter his or her Full name and Email Address for identification purpose.

The system uses the email addresses submitted by users at their first time to post a comment, reply
to a posted comment or like a comment to identify them whenever they try to perform any further
action in the future and as such, users email addresses are unique within the system.
some features of the comment system:

(1) Responsive design

(2) High in security

(3) Users can add their avatar or desired photo for commenting purpose

(4) Enlarge users avatar or photos when clicked on them

(5) Reply to posted comments

(6) Like both comments and replies posted

(7) Email notification for new Replies to users concerned

(8) Send a notification email to admin for every new comment posted for moderation purpose

(9) Nice display of Confirmation Box for comment deletion and logout

(10) Proper validation for empty fields and invalid email addresses

(11) Comments are displayed based on pages where they are posted

(12) No long URLs or contents that extends through the comments box area.

(13) Auto Load more Comments when the page is scrolled down

(14) Nice pagination button for loading replies

(15) Admins can moderate comments by deleting unwanted comments or editing a user comment
that is not formatted properly

(16) Smilies support for comments and replies

(17) You can add multiple photos in comments

(18) You can add videos in comments and supported videos are YouTube, Vimeo, Metacafe, Daily
motion and Flicker.

This powerful comment system supports all modern browsers and programming languages used are
Ajax, Jquery, PHP and MySqli procedural method for the database queries.

The system comes with a language file to enable you easily change the text used throughout the
comment system to suit your needs.

Q.2. How many types of Variables and data types use in PHP Scripts?
Ans 2-Variables in PHP -

PHP also has variables and constants just like other programming languages such as C,
Java, and BASIC. Variables are the storage spaces where you can store values for later
use. They act as identifiers to access memory locations.

In PHP, variable names begin with a dollar ($) sign, whereas assignment is carried out
by using = operator. Furthermore, all statements end up with a semi-colon (;) sign
that indicates the termination of the statement. Another benefit of PHP is that we dont
need to specify the data type using keywords like int, char, float, double or string.

Rules for Variable Declaration

Variable name must always start with a $ sign.

Variable name can never start with a number.

Variable can have an underscore (_) sign or a letter.

Variable name may consist of alphanumeric characters


(e.g: $firstname1, $_lastname2).

Variable names are case-sensitive


(e.g: $lastname or $LASTNAME are not equivalent; both are the
different variables).

You can assign variable names as short or as long as you want


(e.g: $a, $str, $firstname; all are valid).
If a variable is assigned a value more than once, then it should
be the most recent value.

Example:

<?php

$a=5;

$a=$a+5;

echo $a;

?>

Output: 10

In PHP, you dont need to declare data type before assignment.

White spaces are not allowed in declaring PHP variable names.

In PHP, you dont need to know in advance whether you will


store string or numbers.

PHP converts data types itself when needed.

Examples of Variable Declarations

To understand the concept of variable better, here we have an example.

<?php
$integer_num=10; // Integer Value

$float_num=22.3; // Float value

$string_num=Cloudways is a hosting service provider; //String value

$Bool=false; // Boolean value

$null_value=NULL; //Null declaration

?>

Invalid Syntaxes

Like in any other computing language, PHP has its share of invalid syntaxes.

Example

$2name=10; //Invalid because you cant start a variable name with a number.

$Permanent address=7th street, Jackson Apartments, NYC; //Invalid address, cant


use space in a variable name declaration.

$Personal-email=david@gmail.com; // invalid since operators are not allowed in


variable declaration

8 Data Types of PHP

For constructing variables, PHP supports 8 categories of data types.

1. Object
PHP lets you create objects that comprise of functions and variables. Arrays and
functions are examples of objects. We will be discussing this data type in detail when we
cover object-oriented programming in PHP. This data type not only stores data, but it
can also record the information on how to process data.

Example

<?php

class webhosting {

function webhosting() {

$this->servicename = cloudways;

// create an object

$cloud = new webhosting();

// show object properties

echo $cloud->servicename;

?>

Output

cloudways

2. Arrays
Array is another type of variable where you can store a set of values in a single variable.
Arrays are declared using the keyword array. There are 3 different types of arrays:
Indexed arrays: They consists of numeric index.

Associative arrays: These arrays consist of named keys.

Multi-dimensional arrays: These arrays consist of more than


one array.

Example

<?php

$subject = array(calculus, physics, chemistry); // Creating an array $subject for


storing subjects

echo I like . $subject[0] . , . $subject[1] . and . $subject[2] . .;

?>

Output

I like calculus, physics and chemistry.

3. Resources
Resources are special variables dedicated for storing external resources of PHP; for e.g.,
connections of database. I will touch upon resources when covering advanced topics as
it is not an actual data type.

4. Integers
Integers are whole numbers without a decimal point; e.g., 4195. They are the simplest
of the data types. Integers correspond to simple whole numbers, both positive and
negative. Expressions can also be used in variable declaration.

Example
<?php

$int_exp= 345-343;

var_dump($int_exp);

?>

Integers can be declared using hexadecimal, octal and decimal format. By default, it is
in decimal format.

5. Doubles -

All decimal containing numbers or floating-point numbers fall under this data type, for

example, 34.4. It automatically prints the minimum number of decimal places needed.

Example

<?php

$a=44.234224;

$b=5.4320998;

$sum=$a+$b;

echo($a+$b);

?>

Output

49.6663238

6. Boolean
Boolean data type may contain two possibilities, either True or False.
Example

<?php

$a=true;

if(true)

Echo Statement is valid.<br/>;

Else

Echo Invalid statement.<br/>;

var_dump($a); // This function returns the data type of the variable.

?>

Output

Statement is valid

bool(true)

Rules of Boolean Data Type

Value of variable is false if it equals to zero; otherwise, true.

It is false even when the string is empty or zero. Else, it will


remain true.

Null type values are always considered as false.

An array containing no values is false; otherwise, true.

It is prohibited to use floating point numbers as Booleans.


7. Null
It is one special data type whose value is always NULL. Such variables which are
assigned NULL are actually treated as false.

Example

<?php

$empty= null;

var_dump($empty)

?>

Output

NULL

8. Strings
Strings are a collection of characters that can have any text within quotes. You may use
single or double quotes. Object and array are called compound data types, whereas
the other five lie in the category of simple data types.

In PHP, there is no limit with respect to the string length. Escape Character sequences
which contain backslashes are replaced with special characters.

String Character Replacements

Following are the details of string character replacements:

/ will be replaced by a single double quote.

/$ will be replaced by $ sign itself.

/t will be replaced by tab character.


/r will be replaced by carriage return character.

/n will be replaced by new line character.

Example

<?php

$a=Welcome to Cloudways;

echo $a;

?>

Output

Welcome to Cloudways

Q.4. How many types of conditional statement? Explain each in detail.

Ans 4-PHP Conditional Statements


Like most programming languages, PHP also allows you to write code that perform different
actions based on the results of a logical or comparative test conditions at run time. This
means, you can create test conditions in the form of expressions that evaluates to either
true or false and based on these results you can perform certain actions.

There are several statements in PHP that you can use to make decisions:

The if statement
The if...else statement
The if...elseif....else statement
The switch...case statement

We will explore each of these statements in the coming sections.

The if Statement
The if statement is used to execute a block of code only if the specified condition evaluates
to true. This is the simplest PHP's conditional statements and can be written like:
if(condition){
// Code to be executed
}

The following example will output "Have a nice weekend!" if the current day is Friday:

Example
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
}
?>

The if...else Statement


You can enhance the decision making process by providing an alternative choice through
adding an else statement to the if statement. The if...else statement allows you to execute
one block of code if the specified condition is evaluates to true and another block of code if
it is evaluates to false. It can be written, like this:
if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}

The following example will output "Have a nice weekend!" if the current day is Friday,
otherwise it will output "Have a nice day!"

Example
Run this code
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
?>

The if...elseif...else Statement


The if...elseif...else a special statement that is used to combine
multiple if...else statements.
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
}

The following example will output "Have a nice weekend!" if the current day is Friday, and
"Have a nice Sunday!" if the current day is Sunday, otherwise it will output "Have a nice
day!"

Example
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
?>

PHP IfElse Vs SwitchCase


The switch-case statement is an alternative to the if-elseif-else statement, which does
almost the same thing. The switch-case statement tests a variable against a series of values
until it finds a match, and then executes the block of code corresponding to that match.

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
}

Consider the following example, which display a different message for each day.

Example
<?php
$today = date("D");
switch($today){
case "Mon":
echo "Today is Monday. Clean your house.";
break;
case "Tue":
echo "Today is Tuesday. Buy some food.";
break;
case "Wed":
echo "Today is Wednesday. Visit a doctor.";
break;
case "Thu":
echo "Today is Thursday. Repair your car.";
break;
case "Fri":
echo "Today is Friday. Party tonight.";
break;
case "Sat":
echo "Today is Saturday. Its movie time.";
break;
case "Sun":
echo "Today is Sunday. Do some rest.";
break;
default:
echo "No information available for that day.";
break;
}
?>

The switch-case statement differs from the if-elseif-else statement in one important way.
The switch statement executes line by line (i.e. statement by statement) and once PHP finds
a case statement that evaluates to true, it's not only executes the code corresponding to that
case statement, but also executes all the subsequent casestatements till the end of
the switch block automatically.

To prevent this add a break statement to the end of each case block. The breakstatement
tells PHP to break out of the switch-case statement block once it executes the code
associated with the first true case.

Q.5. How many types of loop use in PHP? Explain each in detail.?

Ans 5 - PHP use following four loop types.

(1)PHP while Loop :


In php , the while looping statement will execute a block of code if and as long
as a test expression is a true.

If the test expression is a true then code block will be executed. After the code
has executed the test expression would again be evaluated and the loop would
continue until the test expression is found to be false in php.

In php , A while loop statement is repeatedly executes a target statement as


long as a given condition is true.

Syntax :

while(condition)

statement(s);

increment/decrements;

1.Condition :- the first step of for loop is condition , if condition is true then
body part is execute .otherwise body part is not execute in php .
2.statements :- this part is body part. all three step are execute then body
part is executed .in this part display any type of message in web page using
php.

3.Increment/Decrements :- in while loop , increment and decrements is in


body of program . this step through declare variable increment and decrements
in php (it means declare variable update to the increment and decrements).

Example :-

<?php

int $i;

while($i<3)

echo i am programmer;

$i++;

?>

Output:
i am programmer
i am programmer
i am programmer

(2)PHP dowhile Loop :

do while loop is different loop of the for loop and while loop because , the do
while loop in php language checks its condition at the bottom of the loop.
in php language ,A do while loop is similar to a while loop, except that a do
while loop is guaranteed to execute at least one time.

Syntax :

do

statement(s);

increment/decrements;

}while( condition );

1.statements :- in do while loop , body part is first execute . if condition is true


or false . body part is must be execute one time in do while loop . this part is
body part. all three step are execute then body part is executed .in this part
display any type of message in web page using php .

2.increment/decrements :- this step is also write in body part . this step


through declare variable increment and decrements in php (it means declare
variable update to the increment and decrements).

3.condition :- last step of do while loop is condition , if condition is true then


body part is execute ,otherwise loop is over in php .

Example :

<?php

int $i;
do

echo i am programmer;

$i++;

}while( $i<3 )

?>

Output:
i am programmer
i am programmer
i am programmer

(3)PHP for Loop : In php language, A for loop statement is a repetition control
structure that allows you to efficiently write a loop that needs to execute a
specific number of times. A for loop example and syntax are as bellow.

Syntax :

for ( initialization; condition; increment/decrements )

statement(s);

1.initialization :- this step is a first execute in for loop .initialization step is


allows declaration of variable. this step is starting point of for loop statement in
php.

2.condition :- second step of for loop is condition , if condition is true then


body part is execute .otherwise body part is not execute in php.
3.increment/decrements :- this step through declare variable increment and
decrements in php(it means declare variable update to the increment and
decrements).

4.statements :- this part is body part. all three step are execute then body
part is executed .in this part display any type of message in web page.

Example :

<?php

int $i;

for( $i=1; $i<3; $i++ )

echo i am programmer;

?>

Output:
i am programmer
i am programmer
i am programmer

(4)PHP foreach Loop :The foreach looping statement is used to loop through
a arrays. For each pass the value of the current array element is assigned to
$value and the array pointer is moved by one and in the next pass next element
would be processed in php.
Syntax :

foreach (array as value)

Statement;

1. Array :- array is store different value like 1,2,3,4,, etc type of unindex
value . then one by one are display on web page.

2. Value :- value type variable is store one by one value of array and then after
one by one value is display in web page.

3. Statement :- statement means execute block . his part is body part. all
three step are execute then body part is executed .in this part display any type
of message or value in php.

Example :

<?php

$nums = array( 10, 20, 30, 40, 50);

foreach( $nums as $value )

echo Value is $value <br />;

?>

Output:
Value is 10
Value is 20
Value is 30
Value is 40
Value is 50

Q.8. Write short notes on - (a) PHP 5 (d) Flat File

Ans 8 (a) PHP 5


On July 13, 2004, PHP 5 was released, powered by the new Zend Engine II. [5] PHP 5 included new
features such as improved support for object-oriented programming, the PHP Data Objects (PDO)
extension (which defines a lightweight and consistent interface for accessing databases), and
numerous performance enhancements.[23] In 2008 PHP 5 became the only stable version under
development. Late static binding had been missing from PHP and was added in version 5.3.[24][25]

Many high-profile open-source projects ceased to support PHP 4 in new code as of February 5,
2008, because of the GoPHP5 initiative,[26]provided by a consortium of PHP developers promoting
the transition from PHP 4 to PHP 5.[27][28]

Over time, PHP interpreters became available on most existing 32-bit and 64-bit operating systems,
either by building them from the PHP source code, or by using pre-built binaries. [29] For the PHP
versions 5.3 and 5.4, the only available Microsoft Windows binary distributions were 32-
bit x86 builds,[30][31] requiring Windows 32-bit compatibility mode while using Internet Information
Services (IIS) on a 64-bit Windows platform. PHP version 5.5 made the 64-bit x86-64 builds
available for Microsoft Windows.

Ans 8 (d) Flat File - A flat file database is a database which is stored on its host computer system
as an ordinary unstructured file called a "flat file". To access the structure of the data and manipulate
it, the file must be read in its entirety into the computer's memory. Upon completion of the database
operations, the file is again written out in its entirety to the host's file system. In this stored mode the
database is said to be "flat", meaning that it has no structure for indexing and there are usually no
structural relationships between the records. A flat file can be a plain text file or a binary file.

The term has generally implied a small, simple database. As computer memory has become
cheaper, more sophisticated databases can now be entirely held in memory for faster access. These
newer databases would not generally be referred to as flat-file databases. Flat files are used not only
as data storage tools in DB and CMS systems, but also as data transfer tools to remote servers (in
which case they become known as information streams).

In recent years, this latter implementation has been replaced with XML files, which not only contain
but also describe the data. Those still using flat files to transfer information are mainframes
employing specific procedures which are too expensive to modify.

One criticism often raised against the XML format as a way to perform mass data transfer operations
is that file size is significantly larger than that of flat files, which is generally reduced to the bare
minimum. The solution to this problem consists in XML file compression (a solution that applies
equally well to flat files), which has nowadays gained EXI standards (i.e., Efficient XML Interchange,
which is often used by mobile devices).