You are on page 1of 20

Q1: Answer the following in short

PHP Paper Solution


(a) What is the difference between $name and $$name ?
Ans: $name is a simple variable whereas $$name is a reference variable.
$$name is a variable its name is stored in $name.
Example:
$name = 'test';
$$name = 'xyz';
$test='xyz';
(b) Explain the Define()?
Ans: It is same in c language that it also used to define constant value to the variable
Syntax:
define(name,value,case_sensitive=false)
Name: Required , Specifies the name of the constant
Value: Required, Specifies the value of the constant
case_sensitive: It is optional , It Specifies whether the constant name should be case_insensitive

The defined () function check whether a constant exits


Return True if the constant exits or false otherwise defined

(c) What is different between single quote String literal & double quote String literal?
Ans:
Single Quoted Literal:The simplest way to specify the a string into enclose it in single quote
e.g:- the character
In the echo statement the single quote used to display on the browser windows but variable not expanded
occur in the single quoted
e.g :
$a=10;
echo the number of a = $a;
o/p the number of a = $a
Double Quoted Literal:It the String is enclosed in double quoted()
e.g The String
It is expanded the occur the variable in double quoted literal
e.g
$a=10;
echo the number of a = $a;
o/p the number of a = 10
(d)Explain the terms Die &Return?
Ans :

Die :

The die and Exit statement both are same working

The Die statement terminated execution of the script

It prints status just before exiting

The status is an integer

Status range is 0 to 254

The status 0 is used to terminated the program successfully

Return:The return statement immediately end execution of the current function

The return statement its argument as the value of the function call

Return statement will also end the execution of an eval() statement of an or Script file

(e) What are default session time and path?


Ans:

default session time in PHP is 1440 seconds or 24 minutes


Default session save path id temporary folder /tmp.

(f) What is use of isset() function?


Ans:
In php isset function comes in handy isset is a function that take any variable you want to use and check
to see if it has been set. That is, it has already been assigned a value
eg:
<?php
if(isset ($_session[views]))
$_session[views]=$_session[views]+1;
else

$_session[views]=1;

?>
(g)What is different between function unlink and unset?
Ans :
Unset () Function:- it is mainly used to free the specified session variable
e.g
<?php
unset($_session[views)
?>
Unlink() Function :- Its mainly used delete the file
Syntax :
unlink(filename,context)

Q2: Answer the following

(a) Discuss different Data types in php?


Ans: A Data types refers to the type of data a variable can store. PHP has eight(8) different data types.
1.

Integer number

2.

Floating point number

3.

Strings

4.

Booleans

5.

Arrays

6.

Objects

7.

Resources

8.

Null
Integer

The integer data types is use to specify a numeric value without a factorial comment.

You can declare as given bellow


Integer $varabile;
$variable=10;

Real number

It is also known as floating number or floating point number. It is whole number and has
fractions such as 1.22, 2.45, 100.765 etc.

Some examples of valid floating point numbers include:

3.14

0.001

-1.234

0.314E2 // 31.4

1.234E-5//0.00001234

-3.45E-3//-0.00345

Boolean

Boolean values are true or false, also 0 and empty string evaluates to false, and any numeric value
rather than zero, or a string that is not empty evaluates to true.
You can declare as given bellow.

Boolean $variables;

Where Boolean denotes the type of the variable.

Strings

String values are sequence of characters, include in a single quotes, for example,

$strl = this is a string data type variable;

A string literal can be specified in four different ways:

1.

Single quoted

2.

Double quoted

3.

Heredoc quoted

4.

Now doc quoted


Arrays

An array in PHP is actually ordered map. A map is a type that associates values to keys.

This type is optimized for several different uses; it can be treated as an array, list (vector), hash
table (an implementation of a map), Dictionary, collection, stack, queue, and probably more.
As array values can be other arrays, trees and multidimensional arrays are also possible.

Resource

Resources are not actual data type, but the storing of a reference to functions and resources
external to PHP.
The most common example of using the resources data type is a data base call.

Null

Null is a special data type which can only have one value, which is itself.
Which is to say, null is not only a data type, but also a key word literal. A variable of data type
null is a variable that has no value assigned to it
OR

Q2: Answer the following


(a)Discuss different types of tables in mysql.

Mysql supports various types of tables or storage engines to allow you To optimize your
database.

The table types are available in MYSQL are:


1. ISAM
2. MYISAM
3. InnoDB
4. BerkleyDB(BDB)
5. MERGE
6. HEAP

1. ISAM :

ISAM had been deprecated and removed from version 5.x. all of

It functionality entire replace by MYISAM. ISAM table has a hard Size 4GB and is not portable.

2. MYISAM:

MYISAM table type is default when you create table.MYISAM


Table work very fast but not transaction-safe.The size of MYISAM table depends on the

operating system and the data File is portable from system to system.
With MYISAM table type you can have 64 keys per table and maximum key length of 1024

bytes.

3. InnoDB:
Different from MYISAM table type, innoDB table are transaction safe And supports rowlevel locking.

foreign keys are supported in innoDB Tables.

The data file of Innodb table can be stored in more than one File so the size of table
depends on the disk space.

like the MYISAM Table type, data file of InnoDB is portable from system to system.

The disadvantage of InnoDB in comparison with MyISAM is it take More disk space.

4. BDB:

BDB is similar to InnoDB in transaction safe.


It supports page level locking But data file are not portable.

5. MERGE:

Merge table type is added to treat multiple MYISAM tables as a single Table so it remove
the size limitation from MYISAM tables.
6. HEAP:

Heap table is stored in memory so it is the fastest one. Because of storage Mechanism, the
data will be lost when the power failure and sometime it cause the server run out of memory.
Heap tables do not supports columns with AUTO_INCREMENT, BLOB and TEXT

characteristics.
(b)Discuss different loops available in php.

Loops in PHP are used to execute the same block of code a specified number of times, or while a
specified condition is true.
PHP supports four loop types.

1. for

2. while
3. do...While

4. for each
1. for loop:
The for statements is used when you know how many times you want To execute a statement or
block of statement.
Syntax:
for(initialization; condition; increment)
{
Code to be executed;
}
Example:
<?php
$test =5;
For ($i=1 i<=5 ; i++)
{
echo $i;<br/>;
}
?>
Output:
1
2
3
4
5
2. While loop:

The while loop execute a block of code while a condition is true.


Syntax:
while(condition){
Code to be executed;
}
Example:
<?php
$i =0;
$num =50;
While($i <10)
{
$sum--;
$i++;
}
echo(loop stopped at
?>

i=$i and num =$sum);

Output:
I = 1;
Num = 40

3. Dowhile loop:
The dowhile statement will execute a block of code at least Once it then will repeat the loop as
long as a condition is true.
Syntax:
do
{
Code to be executed;
}while(condition);
Example:
<?php
$i=0;
$num=50;
While($i < 10)
{
$sum;
$i++;

echo(loop stopped at I =$i and num =$num);


?>
Output:
Loop stop at I = 10

4. for each loop:


The foreach statement is used to loop through 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 will be processed.
Syntax:
foreach(array as value)
{
Code to be executed;
}

Example:
<?php
$array = array(1,2,3,4,5);
foreach($array as $value)
{
Echovalue

is

$value <br/>;

}
?>
Output:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5

Q3: Answer the following


(a): Discuss any 5 functions of File handling.
Ans : The filesystem functions are used to access and manipulate the filesystem PHP provides you all the
possible functions you may need to manipulate a file.
1) Fopen() function :
The fopen() function opens a file or URL.
Syntax:
fopen(filename,mode)
Mode:
r = Read only
r+ = Read / Write
w = Write only
w+ = Read / Write
a = Append
x = Create a new file.
Example:<?php
$file=fopen("test.txt","r");

$file=fopen("E:/Sonu/test.txt","r");
?>
2) Fread() function :
The fread() reads from an open file. The function will stop at the end of the file or when it reaches the
specified length,whichever comes first.
Syntax:
fread(file,length)
Example1:<?php
$file=fopen("test.txt","r")
fread($file,"10");
fclose($file);
?>
Example2:<?php
$file=fopen("test.txt","r");
fread($file,filesize("test.txt"));
// for read entire file...
fclose($file);
?>
3) Fwrite() function :
The fwrite() writes to an open file. The function will stop at the end of the file or when it reaches the
specified length,whichever comes first.
Syntax:
fwrite(file,string,length)
length is optional..
Example:<?php
$file=fopen("test.txt","r+");

fwrite($file,"Hello");
fclose($file);
?>
4) Fclose() function :
The fclose() function closes an open file. The function returns TRUE on success or FALSE on failure.
Syntax:
fclose(file)
Example:
<?php
$file=fopen("test.txt","r");
// Some code to be executed
fclose($file);
echo the file is closed
?>
Output:
The file is closed
5) File_exists() function :
The file_exists() function checks whether or not a file or directory exists.
Syntax:
file_exists(path)
Example:<?php
echo file_exists("test.txt");
?>
Output:
The output of the code will be: 1

OR

(A)What is session? Where you will use it in web programming?


A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about
one single user, and are available to all pages in one application.

PHP Session Variables


When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer
knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server
does not know who you are and what you do because the HTTP address doesn't maintain state.

A PHP session solves this problem by allowing you to store user information on the server for later use
(i.e. username, shopping items, etc). However, session information is temporary and will be deleted after
the user has left the website. If you need a permanent storage you may want to store the data in a
database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The
UID is either stored in a cookie or is propagated in the URL.
Why Use Session?
As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where
your website needs to pass along user data from one page to another, it might be time to start thinking
about using PHP sessions.
A normal HTML website will not pass data from one page to another. In other words, all information is
forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which
requires data (the user's selected product) to be remembered from one page to the next.
(b) Write a php script to list data in table from database.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Sample Application</title>
</head>

<body>
<h1 align="center">My First Database Application</h1>
<hr size="3" color="#FF0000"/>
<table align="center">
<tr bgcolor="#CCCCCC">
<td>Name</td>
<td>Age</td>
</tr>
<?php
$query=mysql_query("select * from student");
while($rs=mysql_fetch_object($query))
{
?>
<tr>
<td><?php echo $rs->s_name;?></td>
<td><?php echo $rs->s_age;?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
OUTPUT:

Q.3(B)Write a php script to list databases available in mysql database server.


<?php
$con=mysql_connect("localhost","root","") or die ('Connection
fail..');
$result = mysql_list_dbs($con);
while ($row = (mysql_fetch_object($result)))
{
echo $row->Database."<br>";
}
mysql_close($con);
?>
OUTPUT:information_schema
mysql
performance_schema
test
test_db
Q3(C) How Comments are given in php code?
Ans :

While there is only one type of comment in HTML, PHP has two types.

The first type we will discuss is the single line comment. The single line comment tells the
interpreter to ignore everything that occurs on that line to the right of the comment.

for single line comment type "//" or "#" and all text to the right will be ignored by PHP
interpreter.
PHP Code:

<?php
echo "Hello World!"; // This will print out Hello World!
// echo "Hello India";
# echo "Hello India";
?>
Similar to the HTML comment, the multi-line PHP comment can be used to comment out large blocks of
code or writing multiple line comments. The multiple line PHP comment begins with " /* " and ends with
" */ ".
<?php
echo "Hello World!";
/* echo "Hello India";
echo "Hello World";
*/
?>

Q4: Answer the following


(a) List all input elements. Explain any two with example.
Ans: Input elements are as follows:
There are many input elements for INPUT type like
1. Textbox
2. Hidden fields
3. Password fields
4. Checkbox
5. Radio Button
6. Image
7. Button
8. Submit
9. Reset
OR
(a) How array are created in PHP? Discuss any three sorting Function of array?
Ans .

Arrays provide a way to store much larger amounts of data without having many separate named
variables.

If a normal variable where data can be stored, a simple array is like a row where lots of data can

be stored.

Normal variables have names like $test

An array variable is named with square brackets. Here is an example $test[]

Each element in the array has its own index so that it can be easily accessed.

You can use it either as a simple c like array or as an associative array.

Here array indices are enclosed into [] rather than {}.


Rather than having a fixed number of slots, php creates array slots as new elements are added to

the array.

You can assign any type for keys and values. Such as string, float ,integer etc.

In PHP, there are three kinds of arrays:


o Numeric array - An array with a numeric index
o Associative array - An array where each ID key is associated with a value
o Multidimensional array - An array containing one or more arrays

1.Numeric array

A numeric array stores each array element with a numeric index.


There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):
<?php
$names=array("Juned Sir"," Nishant"," Karishma"," Mayur");
?>
2. In the following example we assign the index manually:
<?php
$names[0]="JunedSir";
$names[1]="Nishant";
$names[2]="Karishma";
$names[3]="Mayur";
?>

2.Associative array :
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to

do it.

With associative arrays we can use the values as keys and assign values to them.
<?php
$ages = array("Nishant"=>20, "Mayur"=>19, "Karishma"=>21);

?>
3.Multidimensional array

In a multidimensional array, each element in the main array can also be an array.
And each element in the sub-array can be an array, and so on.
<?php
$faculties=array
(
"BCA"=>array
(
"JunedSir",
"MaulikSir",
"KirtiMadam"
),
"BBA"=>array
(
"ShamaMadam"
),
"BCOM"=>array
(
"NehaMadam",
"BhumikaMedam",
"RadhikaMadam"
)
);
?>
The array above would look like this if written to the output:
<?php
Array
(
[BCA] => Array
(
[0] => Juned Sir
[1] => Maulik Sir
[2] => Kirti Madam
)
[BBA] => Array
(
[0] => Shama Madam
)
[BCOM] => Array
(
[0] => Neha Madam
[1] => Bhumika Medam
[2] => Radhika Madam
)
)
?>

Three sorting functions of an array :


1) sort()
Sort an array by the values.

Syntax :
sort(array)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
print_r($fruits);
?>
The output of the code above will be:
Array ( [0] => apple [1] => banana [2] => lemon [3] => orange )

2) rsort()

sorts an array by the values in reverse order.


Syntax:
rsort(array)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
print_r($fruits);
?>
The output of the code above will be:
Array ( [0] => orange [1] => lemon [2] => banana [3] => apple )
3) asort()

The asort() function sorts an array by the values. The values keep their original keys.
Syntax:
asort(array)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
asort($fruits);
print_r($fruits);
?>

The output of the code above will be:


Array ( [3] => apple [2] => banana [0] => lemon [1] => orange )
4) arsort()

sorts an array by the values in reverse order. The values keep their original keys.
Syntax:
arsort(array,sorttype)
Example:
<?php
$fruits = array("lemon", "orange", "banana", "apple");
arsort($fruits);
print_r($fruits);
?>
The output of the code above will be:
Array ( [1] => orange [0] => lemon [2] => banana [3] => apple )
Q.4(C) Write a script to fill the combo box from mysql database.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<body>
<select name="mycombo">
<?php
$query=mysql_query("select * from student");
while($rs=mysql_fetch_object($query))
{
?>
<option><?php echo $rs->s_name;?></option>
<?php
}
?>
</select>
</body>
</html>

?
1
Output:

OR
Q.4(B). Write a script for login page with validations.
<?php
$cn=mysql_connect("localhost","root","");
mysql_select_db("test_db",$cn);
?>
<html>
<body>
<h1 align="center">My Login Page</h1>
<hr size="3" color="#FF0000"/>
<script language="javascript">
function checkfrm()
{
if(document.frmlogin.txtlogin.value == "")
{
alert("Please Enter User Name..");
document.frmlogin.txtlogin.focus();
return false;
}
if(document.frmlogin.txtpassword.value == "")
{
alert("Please Enter Password..");
document.frmlogin.txtpassword.focus();
return false;
}

return true;
}
</script>
<?php
if(isset($_POST['submit']))
{
$sql = "select * from login where username='".$_POST['txtlogin']."' and pas
$_POST['txtpassword']."'";
$rs = mysql_query($sql);
if(mysql_num_rows($rs) > 0)
{
header("location:welcome.php");
}
else
{
echo "Invalid Login";
}
}
?>
<form name="frmlogin" method="post" onsubmit="return checkfrm()">
<table align="center">
<tr>
<td>User Name:</td>
<td><input type="text" name="txtlogin" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="txtpassword" /></td>

</tr>
<tr>
<td></td>
<td align="center">
<input type="submit" name="submit" value="Login" />
<input type="reset" /></td>
</tr>
</table>
</form>
</body>
</html>
<strong>Output:</strong>

You might also like