WBP QB Answers $F
WBP QB Answers $F
CHP 1 (2 MARKS):
2. Platform Independence:
PHP-based applications can run on any OS such as UNIX, Windows,
Linux, etc.
3. Database Connection:
It has a built-in database connection that helps to connect databases
and reduce the trouble and the time to develop web applications or
content-based sites altogether.
4. Library Support:
PHP has strong library support, which allows utilizing various function
modules for data representation.
php
<?php
?>
2. Platform Independence:
PHP-based applications can run on any OS such as UNIX, Windows,
Linux, etc.
3. Database Connection:
It has a built-in database connection that helps to connect databases.
4. Library Support:
PHP has strong library support using which one can utilize various
function modules for data representation.
php
<?php
// Array of fruits
?>
php
$a = 10;
1. While Loop:
If the expression/condition specified with while evaluates to true, the
statements inside the loop execute, and control is transferred back to the
expression/condition.
Example:
php
<?php
$a = 1;
$a++;
?>
OR
php
<?php
$a = 1;
Faizan.M
$a++;
endwhile;
?>
2. Do-While Loop:
All the statements inside the loop execute for the first time without
checking any condition. The keyword do passes the flow of control inside
the loop. After executing the loop for the first time, the
expression/condition is evaluated. If it evaluates to true, all statements
inside the loop execute again; otherwise, the loop exits.
Example:
php
<?php
$a = 1;
do {
$a++;
?>
3. For Loop:
The for loop is used when the number of iterations is known in advance.
Example:
php
<?php
}
Faizan.M
?>
4. Foreach Loop:
The foreach loop is specifically used for arrays to iterate over each
element.
Example:
php
<?php
?>
Syntax:
php
if (expression/condition) {
} else {
Example:
php
Faizan.M
<?php
$a = 30;
else
?>
In the above example, the variable $a holds the value 30. The condition checks whether
$a is less than 20. Since it evaluates to false, the output displays: "Variable value a is
greater than 20."
1. Break Statement:
The break keyword is used to terminate and transfer the control to the
next statement when encountered inside a loop or switch case
statement.
Syntax:
php
if (condition) {
break;
Example:
php
<?php
if ($a == 7) {
Faizan.M
?>
2. Continue Statement:
The continue keyword skips the execution of a particular statement
inside the loop and proceeds with the next iteration.
Example:
php
<?php
if ($i == 5) {
?>
4. List loop control structures. Explain any one loop control structure.
ANS:
o While Loop
o Do-While Loop
o For Loop
o Foreach Loop
php
<?php
$a = 1;
$a++;
?>
php
<?php
$n = 1;
do {
echo "$n<br/>";
$n++;
?>
4. Integers:
Integers are whole numbers, such as 1, 12, and 256. Integer literals can be written in
decimal, octal, or hexadecimal.
Example:
php
$decimal = 1998;
5. Floating-Point Numbers:
Represent numeric values with decimal digits. They can be written in general format or
scientific format.
Example:
php
$general = 3.14;
6. Strings:
A string is a sequence of characters, enclosed in either single or double quotes.
Example:
php
$rollNo = 10;
7. Booleans:
Represent true (1) or false (0) values, commonly used in conditional testing.
Example:
php
Faizan.M
$isAvailable = true;
if ($isAvailable) {
echo "Available";
8. Arrays:
Arrays hold multiple values, accessed using either numeric or string keys.
Example:
php
php
Example:
php
<?php
define("SITE_NAME", "MyWebsite");
const PI = 3.14159;
?>
10. Explain the following terms: (i) Variables (ii) Datatypes (iii) Constant (iv)
Operators
ANS:
(i) Variables: Containers for storing data values. In PHP, a variable starts with a $
sign, followed by the variable name.
(ii) Datatypes: Classifications that specify the type of value a variable can hold.
Examples: integers, floating-point numbers, strings, Booleans, arrays, objects,
resources, NULL.
(iii) Constant: An identifier that holds a single value which cannot be changed during
script execution.
Faizan.M
(iv) Operators: Symbols used to perform operations on variables and values. Examples:
Arithmetic operators (+, -, *), comparison operators (==, >), logical operators (&&, ||).
CHP 2 (2 MARKS):
Syntax:
php
2: Returns an array where the key is the position of the word in the
string, and the value is the actual word.
Example:
php
<?php
?>
o Indexed Arrays
Faizan.M
o Associative Arrays
o Multidimensional Arrays
Syntax:
php
Object: An object is an instance of a class. The data associated with an object are
called its properties, and the functions associated with an object are called its
methods. Objects are created using the new keyword.
Syntax:
php
Example:
php
<?php
class student {
public $name;
public $rollno;
Faizan.M
$this->name = $name;
$this->rollno = $rollno;
?>
o Default Function
o Parameterized Function
o Variable Function
Syntax:
php
function functionName() {
// Code to be executed
}
Faizan.M
Example:
php
<?php
function writeMsg() {
writeMsg();
?>
strlen() function:
This function is used to find the number of characters in a string. Spaces between
words are also counted.
Syntax:
php
strlen(string);
Example:
php
<?php
?>
Faizan.M
strrev() function:
This function accepts a string as an argument and returns a reversed copy of it.
Syntax:
php
$strname = strrev(string);
Example:
php
<?php
$str4 = "Polytechnic";
$str5 = strrev($str4);
?>
Examples:
1. Indexed Array:
php
2. Associative Array:
php
$student_one = array("Maths" => 95, "Physics" => 90, "Chemistry" => 96);
3. Multidimensional Array:
Faizan.M
php
$movies = array(
);
Indexed Arrays:
o These arrays can store any type of element, and the index is always a
number.
Array Initialization:
First Method:
php
Second Method:
php
$colors[0] = "Red";
$colors[1] = "Green";
$colors[2] = "Blue";
Example:
php
Faizan.M
<?php
?>
Associative Arrays:
o Associative Arrays store data in key-value pairs, where the key is a string
and the value can be any data type.
o These behave more like two-column tables, where the first column is the
key, and the second is the value.
Example:
php
<?php
$student_one = array("Maths" => 95, "Physics" => 90, "Chemistry" => 96);
$student_two["Maths"] = 95;
$student_two["Physics"] = 90;
$student_two["Chemistry"] = 96;
?>
// Accessing values
<?php
Faizan.M
?>
A constructor is a special built-in method that allows initializing object properties when
an object is created. It executes automatically when an object is created. The
construct method starts with two underscores (__).
Syntax:
php
Example:
php
<?php
class student {
var $name;
function __construct($name) {
$this->name = $name;
function display() {
echo $this->name;
}
Faizan.M
?>
1. str_replace() function:
This function is used to replace characters in a string with other
characters.
Syntax:
php
Example:
php
<?php
?>
Faizan.M
2. ucwords() function:
This function converts the first character of each word in a string to
uppercase.
Syntax:
php
$variable = ucwords($stringvar);
Example:
php
<?php
?>
3. strlen() function:
This function counts the number of characters in a string, including
spaces.
Syntax:
php
strlen(string);
Example:
php
<?php
?>
Faizan.M
4. strtoupper() function:
This function converts all characters in a string to uppercase.
Syntax:
php
$variable = strtoupper($stringvar);
Example:
php
<?php
$str = "polytechnic";
?>
Constructor:
A constructor is a special built-in method that allows initializing object properties when
an object is created. It executes automatically when an object is created. The
__construct() method starts with two underscores (__).
Syntax:
php
Example:
php
Faizan.M
<?php
class Student {
var $name;
function __construct($name) {
$this->name = $name;
function display() {
echo $this->name;
?>
Destructor:
Syntax:
php
function __destruct() {
// Cleanup code
Example:
php
Faizan.M
<?php
class Student {
var $name;
function __construct($name) {
$this->name = $name;
function __destruct() {
?>
1. strrev() function:
This function accepts a string and returns a reversed copy of it.
Syntax:
php
$strname = strrev(string);
Example:
php
<?php
Faizan.M
$str = "Polytechnic";
$reversedStr = strrev($str);
?>
2. strpos() function:
This function is used to find the position of the first occurrence of a
specified word in another string. It returns false if the word is not found. It
is case-sensitive.
Syntax:
php
Example:
php
<?php
?>
1. Install FPDF Library: Download FPDF from fpdf.org and extract it into
your project folder.
Faizan.M
5. Set Font & Style: Use $pdf->setFont(); to define the text font, style, and
size.
Example:
php
<?php
$pdf->addPage();
$pdf->output();
?>
o str_word_count()
o strlen()
o strrev()
o strcmp()
Faizan.M
o strpos()
o str_replace()
o ucwords()
o strtoupper()
o strtolower()
1. str_word_count() function:
Used to count the number of words in a string.
Syntax:
php
Example:
php
<?php
?>
2. strlen() function:
Used to find the number of characters in a string.
Syntax:
php
strlen(string);
Example:
php
<?php
?>
Ans:
php
<?php
class Student {
public $name;
public $rollno;
$this->name = $name;
$this->rollno = $rollno;
function display() {
$s1->display();
Faizan.M
?>
Ans:
php
<?php
function stringLength($str) {
$length = 0;
while (isset($str[$length])) {
$length++;
return $length;
function wordCount($str) {
$wordCount = 0;
$inWord = false;
$inWord = true;
$wordCount++;
$inWord = false;
return $wordCount;
// Example string
?>
Ans:
Associative Arrays
Associative arrays are very similar to numeric arrays in terms of functionality, but they
differ in their index. Associative arrays use strings as their index, allowing a strong
association between keys and values.
Example:
php
<?php
$salaries = array(
);
echo "<pre>";
print_r($salaries);
echo "</pre>";
?>
Multidimensional Arrays
A multidimensional array contains one or more arrays. It has more than one dimension,
structured in rows and columns. Values in a multidimensional array are accessed using
multiple indexes.
Example:
php
<?php
$marks = array(
"chemistry" => 39
),
"chemistry" => 29
Faizan.M
),
"chemistry" => 39
);
echo "<pre>";
print_r($marks);
echo "</pre>";
?>
Ans:
A function is a named block of code that performs a specific task. PHP supports user-
defined functions, where the user can define their own functions.
php
// return $val;
php
Example:
php
<?php
function printMessage() {
printMessage();
?>
As shown in the above program, the printMessage() function is created using the
function keyword. The function prints the message "Hello, How are you?".
Later in the program, when the function is called using printMessage();, it prints the
message, as we can see in the output.
Ans:
Same as before
Faizan.M
Ans:
Same as before
Ans:
Same as before
15. Define function. How to define a user-defined function in PHP? Give an example.
Ans:
Same as before
16. Write a PHP script to sort any five numbers using the array function. *imp
Ans:
<?php $a = array(1, 8, 9, 4, 5); sort($a); foreach($a as $i) { echo $i.' '; } ?>
Ans:
Same as before
Ans:
Definition:
Serializing an object means converting it to a bytestream representation that can be
stored in a file. Serialization in PHP is mostly automatic, it requires little extra work from
you, beyond calling the serialize() and unserialize() functions.
Syntax:
serialize(value);
Faizan.M
Example:
Output:
a:3:{i:0;s:3:"Red";i:1;s:5:"Green";i:2;i:4044;}
Ans:
• It does a shallow copy, so any changes made in the cloned object will not affect
the original object.
• clone is a magic method in PHP. Magic methods are predefined in PHP and
start with “” (double underscore). They are executed in response to some events
in PHP.
Example:
<?php
// Assigning values
$objAnimals->name = "Lion";
print_r($objAnimals);
Faizan.M
print_r($objCloned);
?>
Ans:
Example:
<?php class parentclass { public $roll; public function par_function() {}} class childclass
extends parentclass {public $name; public function child_fun() {}} $obj = new
childclass(); // Class introspection print_r("parent class
exists:".class_exists('parentclass')); echo "<br> child class methods: ";
print_r(get_class_methods('childclass')); echo "<br> child class variables: ";
print_r(get_class_vars('childclass')); echo "<br> parent class variables: ";
print_r(get_class_vars('parentclass')); echo "<br> parent class: ";
print_r(get_parent_class('childclass')); // Object introspection echo "<br> is object: ";
print_r(is_object($obj)); echo "<br> object of a class: "; print_r(get_class($obj)); echo
"<br> object variables: "; print_r(get_object_vars($obj)); echo "<br> methods exist: ";
print_r(method_exists($obj, 'child_fun')); ?>
Ans:
• The parent class is also called a base class or super class. The child class is also
known as a derived class or a subclass.
Faizan.M
• Inheritance allows a class to reuse the code from another class without
duplicating it.
• Reusing existing codes serves various advantages. It saves time, cost, effort, and
increases a program’s reliability.
• To define a class that inherits from another class, you use the extends keyword.
Types of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
Ans:
Same as before
Ans:
php
<?php
// Parent class
class Vehicle {
public $brand;
function displayBrand() {
public $model;
function displayModel() {
?>
Ans:
Overriding occurs when a child class provides its own implementation of a method that
is already defined in the parent class. The method signature (name and parameters) in
the child class must match that of the parent class. By overriding a method, we can
customize the behavior of the method in the child class while retaining the same
method name.
To override a method in PHP, simply declare the method in the child class with the same
name as the parent class's method.
Example:
php
Faizan.M
class ParentClass {
function helloWorld() {
echo "Parent";
function helloWorld() {
echo "\nChild";
$p = new ParentClass;
$p->helloWorld();
$c->helloWorld();
Ans:
php
class shape {
if ($name_of_function == 'area') {
switch (count($arguments)) {
case 1:
Faizan.M
case 2:
$s = new Shape;
// Function call
echo $s->area(2);
echo "\n";
?>
Ans:
Same as before
Ans:
i) Use of serialization.
Serializing an object means converting it to a bytestream representation that can be
stored in a file. Serialization in PHP is mostly automatic, it requires little extra work from
you, beyond calling the serialize() and unserialize() functions.
Faizan.M
Serialize():
• The serialize() function accepts a single parameter which is the data we want to
serialize and returns a serialized string.
Example:
php
<?php
print_r($s_data . "<br>");
$us_data = unserialize($s_data);
print_r($us_data);
?>
Output:
a:3:{i:0;s:7:"Welcome";i:1;s:2:"to";i:2;s:3:"PHP";}
Ans:
Ans:
Same as before
Inheritance allows classes to inherit properties and methods from a parent class,
overloading provides dynamic property and method handling, overriding enables
customization of inherited methods, and cloning creates copies of objects.