PHP Sample Que Solved
PHP Sample Que Solved
1|P age
2|P age
$ob=new student();
echo "This is class";
}
else
{
echo "Not exist";
}?>
d) Differentiate between Session and Cookies.
ANS-
3|P age
<?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
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
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
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