Professional Documents
Culture Documents
PHP QUESTIONS Final (Draft)
PHP QUESTIONS Final (Draft)
1. Advantages of PHP
Ans:
Ans:
Ans:
Ans:
$cookie_name = “user”
$cookie_value = “karan”
Ans:
Ans:
i. GET
a. this method is use to submit HTML form data.
b. This data collected by superglobal variable $_GET
ii. POST
a. This method also use to submit HTML form data.
b. This data collected by superglobal variable $_POST.
Ans:
Ans:
foreach(<array_name> as <accessing_element>)
//body
}
v. Example:
$a = Array(10,20,30,40);
Foreach($a as $n)
Echo $n;
vi. Output:
10
20
30
40
Ans:
Implode Explode
Ans:
i. This method flips array’s key with values and vice versa.
ii. It makes array keys to value
iii. And values to keys of their respective.
iv. Syntax: array_flip(array)
Ans:
Ans:
Ans:
i. Start a session:-
a. Session start using session_start() method.
b. The session_start() function first checks if a session is already
started and if none is started then it starts one.
c. Session variables are set with the PHP global variables
$_SESSION.
d. The $_SESSION[] variables ca be accessed during the
lifetime of session.
e. Example:
<?php
if(isset($_GET['b1']))
{
session_start();
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="dog";
<?php
if(isset($_GET['b1']))
{
session_unset();
session_destroy();
}
?>
<html>
<body>
<form method="get">
<input type="submit" name="b1"
value="Destroy Session">
</form>
</body>
</html>
14. List out database operations.
Ans:
Ans:-
Ans:
i. str_word_count():-
Syntax:
str_word_count(string)
ii. strlen():-
syntax:
strlen(string)
iii. strpos():-
Syntax:-
strpos(string,substring,n)
iv. strtoupper():
Syntax:
strtoupper(string)
17. How do you convert one variable type into another variable in
PHP?
Ans:
Ans:
i. An array can hold many values under a single name, and you
can access the values by referring to an index number
ii. In PHP, the array() function is used to create an array.
iii. In PHP, there are three types of arrays:
1) Indexed arrays - Arrays with a numeric index
2) Associative arrays - Arrays with named keys
3) Multidimensional arrays - Arrays containing one or
more arrays
iv. Syntax:-
$variable = array(List of array);
Ans:
echo Print
i. Echo outputs one or i. Print output only one
more strings or more string or arguments.
arguments.
ii. Echo has no return ii. Print has no return value
value of 1 so it can be used in
expression.
iii. Echo is faster than print iii. Print is slower than
echo
iv. No written with iv. It can or cannot be
paranthesis written with paranthesis
v. Syntax: v. Synytax:
Echo $arg1, $arg2; Print($arg)
20. Describe the string operation in PHP with example.
a. “ . ” :- (concatenation)
i. It is used to add/join more than one strings.
ii. Syntax:-
$a= “string_1”;
$b = “string_2”;
$c = $a.$b;
Ans:
i. setCookie(name,value,expire,path,domain,secure,httponly);
ii. Only the name parameter is required, all other parameters are
optional.
path - This specifies the directories for which the cookie is valid.
domain - The browser will return the cookie only for URLs
within this domain.The default is the server host name.
Ans:
Ans:
class Student{
function getdata(){
$roll=1010;
$name="Dennis";
$marks=98.99;
}
function display(){
print("Student Roll No:". $roll);
print("Student Name:". $name);
print("Student Marks:". $marks);
}
}
24. How can you view the structure of a table?
Ans:
<?php
if(!$conn){
else{
If($result){
while($row = mysqli_fetch_assoc($result)){
echo $row[‘col_name_1’];
echo $row[‘col_name_2’];
}
}
?>
Ans:
Ans:
Ans:
Ans:
i. CHAR(size)
ii. VARCHAR(size)
iii. INT()
iv. DECIMAL()
v. FLOAT()
vi. DATE()
vii. DATETIME()
29. List out Logical operator that used in PHP.
Ans:
Ans:
cookie Session
Small pieces of data sent from A temporary and interactive
website and stored on the user’s information interchange between
computer by the user’s browser two or more communicating
while the user is browsing devices or between a computer or
user.
Stored in the client’s browser as Stored in the server side
text files.
Can store a minimum amount of Can store a large amount of data
data
Provide minimum security Provide more security beacuase it
beacuase it is easier to access is difficult to access session value
cookies value
Ans
$_GET:-
$_POST:-
Ans:
Final class:
Final method:
Ans:
i. Integer:
i. It is a collection of 0 to 9 digits combination value without
decimal points.
ii. You can represent Integer number in decimal,octal and
hexadecimal format.
iii. If it is decimal, it is just the number.
For example: 145
iv. In PHP, we can test whether value is int or not by using
is_int() or is_integer()
v. Example:
<?php
//Integer data Type
$num=0x34;
if(is_int($num))
{ echo "Number is integer"; }
Else
{ echo "Number is not integer"; }
?>
ii. Floating Point Number:
i. Floating point number is called as real number.
ii. It is a collection of 0 to 9 digits combination value with
decimal points.
iii. In PHP, floating point number will display value upto 15
digits decimal places.
iv. You can represent floating point number using two
different format.
I) Fractional Format : 23.45, 67.8, 143.78
II) Exponential Format: 1.3E-56, 2.3E89
v. In PHP, we can test whether value is float or not by using
is_float() or is_real()
vi. Example:
<?php
//float data Type
$num=3.14;
if(is_float($num))
{
echo "Number is float";
}
Else
{
echo "Number is not float";
}
?>
iii. String:
i. Collection of characters is known as String.
ii. We can represent string using single or double quotes.
iii. PHP provided method is_string() to check whether given
value is string or not.
iv. Exmple:
<?php
$name="VJTech";
//echo "My class name is $name";
if(is_string($name))
{
echo "$name is String";
}
else
{
echo "$name is not String";
}
?>
iv. Boolean:
i. Boolean value can be either TRUE or FALSE
value.
ii. Both values are case-insensitive.
iii. is_bool() method used to check whether value is
boolean or not.
iv. Example:
<php
$m=true;
if(is_bool($m))
{
echo "$m is Boolean";
}
Else
{
echo "$m is not Boolean";
}
?>
v. Arrays:
i. Array is used to store multiple values in single variable.
ii. In PHP,Collection of different types of values is known as
Array.
iii. Array index should begin with 0 and end with SIZE-1.
iv. We can retrive the individual array elements using subscript
and index number.
v. Example:
<?php
$p=array('a','b','c','d');
$q=array('first'=>'10','second'=>'20','third'=>'30');
echo "Value of p array 0 index = $p[0]";
echo "Value of p array 1 index = $p[1]";
echo "Value of p array 2 index = $p[2]";
echo "Value of q array = ".$q['first'];
echo "Value of q array = ".$q['second'];
echo "Value of q array =".$q['third'];
?>
34. Explain different types of array in php.
Ans:
1) Indexed arrays :-
i. There are two ways to create indexed arrays.
ii. The index can be assigned automatically(index always
starts at 0),
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
2) Associative arrays :-
i. Associative arrays are arrays that use named keys that you
assign to them.
ii. There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$num = array("a"=>123,"b"=>345,"c"=>789);
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
iii. Example:
<?php
$num = array("a"=>123,"b"=>345,"c"=>789);
echo "Array Elements are ".$num['a']." ".$num['b']."
".$num['c'];
?>
3) Multidimensional arrays :-
i. Arrays containing one or more arrays.
ii. Values in the multi-dimensional array are accessed
using multiple index.
iii. Example:
$a=array(10,20,30);
$b=array(40,50,60);
$c=array(70,80,90);
$multiArray=array($a,$b,$c);
$x=multiArray[2][1];
Ans:
Ans:
Ans:
i. Indexed Array:
a. In this type of array we enter the elements with respect to
index.
b. In it each element has its own unique index.
c. This indexes starts from 0
d. We can access the element of this array using this
indexes.
e. Example:
i. $arr = array([0] => ‘karan’, [1] => ‘bhale')
Ans:
i. It’s a ability of program to examine object characteristics.
ii. Such as its name, parent class, classes, interface etc.
iii. PHP has large number of functions for this task.
iv. Following are functions of introspection:
a. Class_exists(): checks whether class is present or not.
b. Et_class(): return class name of object
c. Get_parent_class(): return parent class of object’s class.
d. Is_subclass_of(): checks whether given object has
given parent class.
e. Get_class_vars(): Return default properties of class.
f. Interface_exists(): check whether interface defined
3. Session vs cookies:
Ans:
Session Cookies
Ans:
Implode Explode
Ans:
<?php
class Area
public $radius;
function get_radius(){
var $PI;
function Calc_Area_Of_Circle()
$this->PI=3.14;
$Area=($this->PI*$this->radius*$this->radius);
}
$c1=new Circle();
$c1->get_radius();
$c2->Calc_Area_Of_Circle();
?>
Ans:
i. php session is use to store and pass the information from one
page to another securely.
ii. It’s a user data which is store on a server side.
iii. Sessions provides a security to user profile.
iv. Php session creates unique id for each browser to recognize
the user.
v. We can start session in php using session_start() method.
vi. After this method we can set our session variables using this
$_SESSION super global variable.
vii. This variable use to access session variables.
viii. We can unset all session variables using session_unset()
method
ix. Then we can destroy the session using session_destroy()
method.
x. Example:
7. Write update and delete operation on table data
Ans:
i. Update operation:
a. We can update the data of table using this operation.
b. We use update query for doing this operation.
c. Syntax: Update <table_name> set <column_name> =
<value> where <condition>;
d. Example:
Ans:
i. Var_dump():
a. Dumps the information about one or more variable:
b. Syntax: var_dump(<variable>)
c. Example:
$a = 10;
Echo var_dump($a);
d. Output: int(10)
ii. Is_int():
a. Checks whether the variable is integer.
b. Syntax: is_int(<variable>)
c. Example:
$a = 20;
Echo is_int($a);
d. Output: 1
iii. Is_float():
a. Check whether the variable is float
b. Syntax: is_float(<variable>)
c. Example:
$a = 10.20;
Echo is_float($a);
d. Output: 1
iv. is_null():
a. checks the variable is null or not
b. Syntax: is_null(<variable>)
c. Example:
$a = null;
Echo is_null($a);
d. Output: 1
v. is_iterable():
a. checks the content of variable is iterable or not
b. Syntax: is_iterable(<variable>)
c. Example:
$a = array(10,20,30);
Echo is_iterable($a);
d. Output: 1
Ans:
i. Definition:
a. The process of converting object into a sequence of bits.
b. By which we can easily stored in file, buffer and transmit
through network.
c. We use Serialize() and unserialize() function for
converting object to sequence of bits and reverse.
ii. Serialize() function:
a. It return bit wise sequence of given object. In string format
b. Syntax: serialize(<variable>);
c. Example: $a = “karan”;
$ser = serialize($a);
Echo $ser;
d. Output:
I:0,s:5:”karan”;
iii. unSerialize():
a. it return the normal variable value.
b. It convert serialize string into actual value.
c. Syntax: unserialize(<variable>);
d. Example: $a = “karan”;
$ser = serialize($a);
$norvar = unserialize($ser);
Echo $norvar;
e. Output:
Karan
10. Explain insertion of data and retrieving data from table
Ans:
I. Insertion:
a. Using this operation we can add data in table.
b. Using this operations we can add data in particular column
or in all column.
c. For this we use insert query of SQL.
d. Syntax:
i. Insert into <table_name> values (<values>);
ii. Insert into <table_name> <column_names> values
(<values>);
e. Example: insert into data (name,age) values (‘karan’, 20);
II. Retrieving:
a. Using this operations we can retrieve data from table.
b. We can get this values in form of associative array.
c. For this we use ‘select query’ of SQL
d. Syntax:
i. Select * from <table_name>;
ii. Select <column_names> from <table_name>;
e. Example: select * from data;
11. Create webpage using GUI component
Ans:
<html>
<body>
</form>
</body>
</html>
Ans:
<?php
Include “FPDF/fpdf.php”;
$pdf->AddPage();
$pdf ->output();
13. Describe following string functions:
a. Str_replace():
b. Ucword():
c. Strlen():
d. Strtoupper():
Ans:
i. Str_replace():
a. This function use to replace current characters of string to
new one.
b. In it we need to specify current sub string, new sub string,
original string.
c. Syntax:
i. Str_replace(<find>, <replace>, <original_string>,
count);
ii. Ucword():
a. This function use to convert given sentence into sentence
case.
b. In it first character of each word become capital.
c. Syntax: ucword(<string>);
iii. Strlen():
a. This function return the length of string.
b. In it we need to specify the string. And get integer value in
return.
c. Syntax: strlen(<string>);
iv. Strtoupper():
a. This function use to convert given string in to upper case.
b. In it we specify original string and get new string in
uppercase.
c. Syntax: strtoupper(<string>);
Ans:
Ans:
<?php
// Free memory
imagedestroy($image);
?>
16. Describe cookies:
Ans:
i. Cookies:
a. Cookies are small piece of data sent from browser and
stored on user side.
b. It stored in text file.
c. It only stays until its expiration period.
d. We can set its expiration period while declaring it.
ii. How to set cookies:
a. For setting cookies we use setcookies() method.
b. Here we give name, value, time, path as parameters
c. Syntax: setcookies(<name>, <value>,<time>,<path>);
iii. How to modify cookies:
a. For modifying cookies we use $_COOKIE super global
variable
b. Using this we can modify and access the cookies values.
c. Syntax: $_COOKIE[<cookie_name>];
iv. How to delete cookies:
a. For deleting cookies we reduce their expiration period.
b. Such as we give a negative time for cookie so that it can
delete automatically.
17. Describe web page validation with example:
Ans:
Ans:
19. Explain implode explode function in php
Ans:
i. Implode:-
a. This function use to convert array into string.
b. This function in php use to join element of an array
with string.
c. It creates string from array.
d. This function join array elements with sep(separator)
string.
e. Syntax:
string implode(string sep,array $name);
f. Example:-
<?php
$city=array("Pune","Thane","Nashik","N
agar","Tasgaon"); $str=implode("-
",$city);
echo "String Values : $str";
?>
ii. Explode:-
a. It breaks string into smaller part and strored it in an
array.
b. To convert string into an array we use explode
function.
c. This function split the string based on the delimiter
and return an array.
d. - Syntax:
array explode(string $delimiter,string str[,int
$limit]);
e. $str="one|two|three|four|five";
$NumArray=explode("|",$str);
print_r($NumArray);
Ans:
Ans:
//body
viii. Example:
<?php
Class shape{
Function __call($name, $args){
If($name == ‘area’){
Switch(count($args)){
Case 0 : return 0;
Case 1: return args[0] * args[0];
Case 2: return args[0] * args[1];
}
}
}
$S = new shape();
$ar = s->area(5);
$ar = s->area(5,10);
Ans: