MSBTE Solution App
MSBTE Solution App
1|P age
g) State the use of “$” sign in PHP.
ANS-
The “$” sign is used for declaring a variable in PHP.
Example: $name= "Viji"
Q.2) Attempt any THREE of the following. (12 Marks)
a) Write a program using foreach loop.
ANS-
<?php
$season = array ("Summer", "Winter", "Autumn", "Rainy");
foreach ($season as $element) {
echo "$element";
echo "</br>";
}
?>
b) Explain Indexed and Associative arrays with suitable example.
ANS-
• Indexed array are the arrays with numeric index. These arrays can store numbers,
Strings and any object but their index will be represented by numbers by default, the
array index starts from 0.
<?php
$a=array(22,33,"sss","v");
echo "Accessing the array elements directly:</br>";
echo $a[2], "</br>";
echo $a[0], "</br>";
echo $a[1], "</br>";
?>
• Associative arrays are used to store key value pairs. Associative arrays have strings
as keys and behave more like two-column tables. The first column is the key, which
is used to access the value.
<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);
echo $ages["Clark"],"</br>";
echo $ages["Peter"],"</br>";
echo $ages["John"];
?>
c) Define Introspection and explain it with suitable example.
ANS-
Introspection is the ability of a program to examine an object's characteristics, such as
its name, parent class (if any), properties, and methods.
With introspection, we can write code that operates on any class or object.
<?php
class student
{
}
if(class_exists('student'))
{
2|P age
$ob=new student();
echo "This is class";
}
else
{
echo "Not exist";
}?>
d) Differentiate between Session and Cookies.
ANS-
3|P age
Q.3) Attempt any THREE of the following. (12 Marks)
a) Differentiate between implode and explode functions.
ANS-
<?php <?php
$a[0]="chocolate"; $text="chocolate,strawberry,vanila";
$a[1]="strawberry"; $a=explode(",",$text);
$a[2]="vanila"; print_r($a);
$text=implode(" <br>",$a); ?>
echo $text;
?>
5|P age
Delete: Data can be deleted into MySQL tables by executing SQL DELETE
statement through PHP function mysql_query().
<?php
$host='localhost:3306';
$user='root';
$pass='';
$db='product';
$conn=mysqli_connect($host,$user,$pass,$db);
if(!$conn)
{
die("fail to connect".mysqli_connect_error());
}
echo"connected successfully";
echo"<br>";
$sql="delete from prod where pid=222";
if(mysqli_query($conn,$sql))
echo("data deleted");
else
echo"could not insert record",mysqli_error($conn);
mysqli_close($conn);
?>
Q.4) Attempt any THREE of the following. (12 Marks)
a) State the variable function. Explain it with example.
ANS-
PHP supports the concept of variable functions. This means that if a variable name
has parentheses appended to it, PHP will look for a function with the same name as
whatever the variable evaluates to, and will attempt to execute it.
<?php
function add($a,$b)
{echo "addition =",$a+$b,"</br>";
}
function sub($a,$b)
{echo "sutraction",$a-$b."</br>";}
function fun($string)
{ echo $string;}
$f = 'add';
$f(2,3); // This calls add()
$f='sub';// This calls sub()
$f(6,3);
$f='fun';
$f('test'); // This calls fun()?>
?>
6|P age
b) Explain the concept of Serialization with example.
ANS-
Serialization is a technique used by programmers to preserve their working data in a
format that can later be restored to its previous form. Serializing of on object means
Converting it to a byte Stream representation that can be stored in a file
1 . Serialize () :
- The Serialize () used to Convert array into byte stream
- It is also used to Convert object into byte stream.
Program :
<?php
$a = serialize(array("Red", "Green", "Blue"));
echo $a;
?>
2. unserialize ( ) ;
- The unserialize () used to convert byte stream into array
- It is also used to convert byte stream into object
Program :
<?php
$data = serialize(array("Cricket", "Football", "Hockey"));
echo $data;
echo "<br>";
$un = unserialize($data);
print_r($un);
?>
7|P age
ii. Destroy session
To remove all global variables and destroy the session, session_destroy() function is
used. It does not take any arguments and a single call can destroy the session.
Program:
<?php
session_start();
$_SESSION["favcolor"]="green";
echo "Your favourite colour is ".$_SESSION["favcolor"];
session_destroy();
?>
Retrieve the query result: Data can be retrieved into MySQL tables by executing SQL
SELECT statement through PHP function mysql_query().
<?php
$h="localhost:3306";
$u="root";
8|P age
$p="";
$db="";
$conn=mysqli_connect($h,$u,$p,$db);
if(!$conn){
die("Database cannot connect");
}
else{
echo "Database connected<br>";
}
$sql='select * from sample';
$val=mysqli_query($conn,$sql);
if(mysqli_num_rows($val)>0){
while($row=mysqli_fetch_assoc($val)) {
echo "ID: {$row['ID']}<br>";
echo "Name: {$row['Name']}<br>";
echo "Salary: {$row['Salary']}<br>";
}}
else{
echo "No results";
}
mysqli_close($conn);
?>
e) Create a web page using GUI components.
ANS-
<html>
<body>
<form name="f1" method="post" action="">
Enter Your Name here:<input type="text" name="name">
<br><br>
Enter Your Address Here:<textarea name="textarea"></textarea>
<br><br>
Gender:
<input type="radio" name="m">Male
<input type="radio" name="f">Female
<br><br>
Hobbies:
<input type="checkbox" name="check"> Dancing
<input type="checkbox" name="check"> Playing
<input type="checkbox" name="check"> Singing
<br>
<td>
<label>Course</label>
</td>
<td>
<select name="course">
9|P age
<option>IF6I</option>
<option>C06I</option>
<option>ME6I</option>
<option>CE6I</option>
</select>
</td>
<br>
<input type="submit" name="Submit ">
</form>
</body>
</html>
10 | P a g e
echo "Database Connected";
}
mysqli_close($conn);
?>
c) Explain the concept of overriding in detail.
ANS-
In function overriding, both parent and child classes should have same function name
with and number of arguments. It is used to replace parent method in child class. The
purpose of overriding is to change the behavior of parent class method. The two
methods with the same name and same parameter is called overriding.
<?php
class p
{
function dis1()
{
echo "This is parent class<br>";
}
}
class child extends p
{
function dis1()
{
echo "This is child class";
}}
$p=new p();
$p->dis1();
$c=new child();
$c->dis1();
?>
12 | P a g e
i. __call()
The – call() method is invoked automatically when a non- existing method or
inaccessible method is called
Syntax :
public function --call ( $name. $arguments)
{
// body of -- call ( )
}
Here. It has two arguments,
$name - name of the method being called by object
$arguments - array of arguments passed to method call.
- In PHP , function overriding is done with the help of magic function -_call ()
ii. mysqli_connect()
The mysqli_connect() function is used to connect with MySQL database. It
returns
resource if connection is established or not.
Syntax:
mysqli_connect(server, username, password, database_name)
Program:
<?php
$host="localhost";
$user="root";
$pass="";
$conn=mysqli_connect($host,$user,$pass);
if(!$conn){
die("Could Not Connect database");
}
else{
echo "Database Connected";
}
mysqli_close($conn);
?>
13 | P a g e