php solved QB
php solved QB
Chapter 01
1. State the features of php.
1. Web Application Features
2. Command line interface(CLI)
3.Built-in Modules
4.Object Oriented Programming (oop)
5.pre-compilation
6. Real Time acces monitoring
7. File I/O
Float : Floating point numbers represents numeric values with decimal points (real numbers)
or a number in
exponential form. The range of floating point values are 1.7E-308 to 1.7E+308. Example:
3.14, 0.25, -5.5, 2E-4, 1.2e2
String : A string is a sequence of characters. A string are declares using single quotes or
double quotes. Example:
“Hello PHP”, ‘Hello PHP’
Boolean : The Boolean data types represents two values, true(1) or false(0). Example:
$a=True
Object :
Objects are defined as instances of user defined classes that can hold both values and
functions. First we declare class
of object using keyword class. A class is a structure that contain properties(variables) and
methods(functions).
10. Write a PHP program to display numbers from 1-10 in a sequence using for loop.
PHP Code-
<?php
echo "Output<br>";
for($i=1;$i<=10;$i++)
{
echo "$i<br/>";
}
?>
Output
1
2
3
4
5
6
7
8
9
10
11. Describe the syntax of if-else control statement with example in PHP.
if-else control statement is used to check whether the
condition/expression is true or false. Ifthe expression / condition
evaluates to true then true block code associated with the if statement
is executed otherwise if it evaluates to false then false block of code
associated with else is executed.
Syntax:
if (expression/condition)
{
True code block;
}
else
{
False code block;
}
Example:
<?php
$a=30;
if ($a<20)
echo "variable value a is less than 20";
else
echo "variable value a is greater than 20";
?>
In the above example, variable a holds value as 30. Condition checks
whether the value of a is less than 20. It evaluates to false so the
output displays the text as ‘variable value a is greater than 20’
<?php
$a = 20;
$b = 10;
echo "First number:$a <br/>";
echo "Second number:$b <br/>";
echo "<br/>";
$c = $a + $b;
echo "Addtion: $c <br/>";
$c = $a - $b;
echo "Substraction: $c <br/>";
$c = $a * $b;
echo "Multiplication: $c <br/>";
$c = $a / $b;
echo "Division: $c <br/>";
$c = $a % $b;
echo "Modulus: $c <br/>";
?>
Output
First number:20
Second number:10
Addtion: 30
Substraction: 10
Multiplication: 200
Division: 2
Modulus: 0
14. Explain logical operators with example. (Any 4)
Logical operators are basically used to operate with conditional statements and
expressions. Conditional statements are based on conditions. Conditional statement
can either be true or false.
<?php
$a = 20;
$b = 10;
if ($a == 20 and $b == 10)
echo "True <br/>";
if ($a == 20 or $b == 30)
echo "True <br/>";
if ($a == 20 xor $b == 5)
echo "True <br/>";
if ($a == 20 && $b == 10)
echo "True <br/>";
if ($a == 20 || $b == 20)
echo "True <br/>";
?>
Output :
True
True
True
True
True
Chapter 02
Associative-
<?php
$capital = array("Mumbai" => "Maharashtra","Goa" => "Panaji", "Bihar" =>
"Patna");
print_r($capital);
echo"<br>";
echo "Mumbai is a capital of " .$capital ["Mumbai"];
?>
Output :
Array ( [Mumbai] => Maharashtra [Goa] => Panaji [Bihar] => Patna )
Mumbai is a capital of Maharashtra
<?php
$num = array(40, 61, 2, 22, 13);
echo "Before Sorting:<br>";
$arrlen= count($num);
for($x = 0; $x < $arrlen; $x++)
{
echo $num[$x];
echo "<br>";
}
sort($num);
echo "After Sorting in Ascending order:<br>";
$arrlen= count($num);
for($x = 0; $x < $arrlen; $x++)
{
echo $num[$x];
echo "<br>";
}
echo "After Sorting in Descending order:<br>";
rsort($num);
$arrlen= count($num);
for($x = 0; $x < $arrlen; $x++)
{
echo $num[$x];
echo "<br>";
}
?>
Output :
Before Sorting:
40
61
2
22
13
After Sorting in Ascending order:
2
13
22
40
61
After Sorting in Descending order:
61
40
22
13
2
6. Explain PHP string functions with example. (Any 4)
7. Define function. How to define user defined function in PHP? Give example.
PHP functions are similar to other programming languages. A function is a piece of
code which takes one more input
in the form of parameter and does some processing and returns a value.
They are built-in functions but PHP gives you option to create your own functions
as well. A function will be executed
by a call to the function. You may call a function from anywhere within a page.
There are two parts which should be clear to you
o Creating a PHP Function
o Calling a PHP Function
It’s very easy to create your own PHP function. Suppose you want to create a PHP
function which will simply write a
simple message on your browser when you will call it. Following example creates a
function called writeMessage()
and then calls it just after creating it.
A user-defined function declaration starts with the word function :
Syntax :
function functionName()
{
code to be executed;
}
Web Based Application Development using PHP (MSBTE) 2-27 Arrays, Functions
and Graphics
Example :
<html>
<head>
<title>Writing PHP Function</title>
</head>
<body>
<?php
/* Defining a PHP Function */
function writeMessage(){
echo "Welcome to PHP world!";
}
/* Calling a PHP Function */
writeMessage();
?>
</body>
</html>
Output :
Welcome to PHP world
8. Write a PHP program to (i) Calculate length of string (ii) Count number of
words in the string
i) Calculate length of string
<?php
$str = 'Have a nice day ahead!';
echo "Input String is:".$str;
echo "<br>";
echo "Length of String str:".strlen($str);
// output =12 [including whitespace]
?>
ii) Count number of words in string
Solution1-
<?php
// PHP program to count number of
// words in a string
$str = " This is a string ";
// Using str_word_count() function to count number of words in a
string
$len = str_word_count($str);
// Printing the result
echo "Number of words in string : $len";
?>
Output:
Number of words in string : 4
OR
Solution 2:
<?php
// PHP program to count number of
// words in a string
$string = " This is a string ";
$str = trim($string);
while (substr_count($str, " ") > 0) {
$str = str_replace(" ", " ", $str);
}
$len = substr_count($str, " ")+1;
// Printing the result
echo "Number of words in string: $len";
?>
Output:
Number of words in string: 4
9. Define Array. State its types.
Arrays in PHP is a type of data structure that allows to store multiple elements of
similar data type under a single
variable thereby saving us the effort of creating a different variable for every data.
An array in PHP is actually an ordered map. A map is a type that associates values
to keys.
The arrays are helpful to create a list of elements of similar types, which can be
accessed using their index or key.
Suppose we want to store five names and print them accordingly. This can be easily
done by the use of five different
string variables. But if instead of five, the number rises to hundred, then it would be
really difficult for the user or
developer to create so much different variables. Here array comes into play and helps
us to store every element
within a single variable and also allows easy access using an index or a key.
An array is created using an array() function in PHP.
Types of Array :
There are basically three types of arrays in PHP :
1. Indexed or Numeric Arrays
2. Associative Arrays
3. Multidimensional Arrays
Explode()
The explode() function breaks a string into an array.
The explode() is a built in function in PHP used to split a string in different strings.
The explode() function splits a string based on a string delimeter, i.e. it splits the
string wherever the delimeter
character occurs. This function returns an array containing the strings formed by
splitting the original string.
Syntax :
array explode(separator, OriginalString, NoOfElements);
The explode( )function accepts three parameters of which two are compulsory and
one is optional. All the three
parameters are described as follows :
1. separator : This character specifies the critical points or points at which the string
will split, i.e. whenever this
character is found in the string it symbolizes end of one element of the array and start
of another.
2. OriginalString : The input string which is to be split in array.
3. NoOfElements : This is optional. It is used to specify the number of elements of the
array. This parameter can be any
integer ( positive , negative or zero)
(i) Positive (N) : When this parameter is passed with a positive value it means that the
array will contain this
number of elements. If the number of elements after separating with respect to the
separator emerges to be
greater than this value the first N-1 elements remain the same and the last element is
the whole remaining
string.
(ii) Negative (N) : If negative value is passed as parameter then the last N element of
the array will be trimmed
out and the remaining part of the array shall be returned as a single array.
(iii) Zero : If this parameter is Zero then the array returned will have only one element
i.e. the whole string.
When this parameter is not provided the array returned contains the total number of
element formed after
separating the string with the separator.
Example :
<html>
<body>
Web Based Application Development using PHP (MSBTE) 2-14 Arrays, Functions
and Graphics
<?php
$str = "PHP: Welcome to the world of PHP";
print_r (explode(" ",$str));
?>
</body>
</html>
Output :
Array ( [0] => PHP: [1] => Welcome [2] => to [3] => the [4] => world [5] => of [6]
=> PHP )
Chapter 03
Destructors
<?php
class MyDestClass
{
function __construct()
{
print "In constructor<br>";
}
function __destruct()
{
print "Destroying " . __CLASS__ . "<br>";
}
}
$obj = new MyDestClass();//object is not created
?>
3. Write a PHP script Create a class as “Rectangle” with two properties as length
and width. Calculate area of rectangle for 2 objects.
<?php
class Rectangle
{
// Declare properties
public $length = 0;
public $width = 0;
// Method to get the area
public function getArea()
{
return ($this->length * $this->width);
}
}
// Create multiple objects from the Rectangle class
$obj1 = new Rectangle;
$obj2 = new Rectangle;
// Call the methods of both the objects
echo "Area of Object1:" . $obj1->getArea() . "<br>"; // Output: 0
echo "Area of Object2:" .$obj2->getArea(). "<br>"; // Output: 0
// Set $obj1 properties values
$obj1->length = 30;
$obj1->width = 20;
// Set $obj2 properties values
$obj2->length = 35;
$obj2->width = 50;
// Call the methods of both the objects again
echo "After calling”;
echo "Area of Object1:" .$obj1->getArea() . "<br>"; // Output: 600
echo "Area of Object2:" .$obj2->getArea() . "<br>"; // Output: 1750
?>
Output :
Area of Object1:0
Area of Object2:0
Area of Object1:600
Area of Object2:1750