0% found this document useful (0 votes)
14 views5 pages

WPB Ut1

The document contains a question bank for PHP, including 2-mark and 4-mark questions covering various topics such as advantages of PHP, variable types, data types, functions, and decision-making statements. It provides examples and explanations for concepts like arrays, bitwise operators, and a PHP program to calculate factorial. Overall, it serves as a study guide for PHP programming fundamentals.

Uploaded by

riddish09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

WPB Ut1

The document contains a question bank for PHP, including 2-mark and 4-mark questions covering various topics such as advantages of PHP, variable types, data types, functions, and decision-making statements. It provides examples and explanations for concepts like arrays, bitwise operators, and a PHP program to calculate factorial. Overall, it serves as a study guide for PHP programming fundamentals.

Uploaded by

riddish09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UT1 Question Bank

2 Marks Questions:

1) List any four advantages of PHP.

Ans: Here are the advantages of PHP in short:

1. Easy to Learn – Simple syntax and beginner-friendly.


2. Platform Independent – Runs on Windows, Linux, and macOS.
3. Strong Community Support – Large developer community and documentation.
4. Database Connectivity – Supports MySQL, PostgreSQL, etc.
5. Open-Source – Free to use and modify.
6. Fast Performance – Executes scripts quickly.
7. Framework Support – Laravel, CodeIgniter, Symfony, etc.
8. HTML Integration – Easily embedded in HTML.
9. Library Support – Built-in functions for various tasks.
10. Security Features – Protects against SQL injection, XSS, etc.
11. Scalability – Suitable for large applications.

2) Give the example of local and global variable.

Ans: {Global Variable – Defined outside the function and can be accessed anywhere in the script.
Local Variable – Defined inside the function and can only be accessed within that function.}
<html>
<body>
<?php
$globalVar = “I am a global variable”; // Global variable
Function local() {
$localVar = “I am a local variable”; // Local variable
Echo $localVar;
}
local();
echo $globalVar;
?>
</body>
</html>

3) List any four data types of PHP.

Ans: PHP supports the following data types:


1. String
2. Integer
3. Float
4. Boolean
5. Array
6. Object
7. NULL
4) What is a function and its types in PHP?
Ans:i) A function in PHP is a block of reusable code that performs a specific task when called.
ii) Types of Functions in PHP:
1. Built-in Functions
2. User-defined Functions
3. Anonymous Functions
4. Arrow Functions

5) Why PHP is known as scripting language?


Ans: PHP is called a scripting language because:
I. It is interpreted rather than compiled.
II. It runs inside a server to generate dynamic content.
III. It is embedded within HTML to create web pages.
IV. It executes line by line on the server before sending output to the browser.

6) Define implode() and explode() function in PHP.


Ans: i) implode() – Converts an array into a string using a separator.
Example
$arr = [“Hello”, “World”];
Echo implode(“ “, $arr); // Output: Hello World

ii) Explode() – Splits a string into an array based on a delimiter.


Example
$str = “Hello,World”;
Print_r(explode(“,”, $str)); // Output: Array ( [0] => Hello [1] => World )

4 Marks Questions:

1) What is an Array and its types with example in PHP?


Ans: i)An array in PHP is a data structure that stores multiple values in a single variable.
ii)Types of Arrays in PHP:
a) Indexed Array –
i)Stores elements with numeric indexes.
ii) Useful when the order of elements matters.
iii) Example:
<?php
$fruits = [“Apple”, “Banana”, “Cherry”];
Echo $fruits[0]; // Output: Apple
?>

b) Associative Array –
i) Uses named keys instead of numeric indexes.
ii) Helpful for storing key-value pairs, such as user data.
iii) Example:
<?php
$person = [“name” => “John”, “age” => 25];
Echo $person[“name”]; // Output: John
?>

c) Multidimensional Array –
i) Contains arrays inside an array.
ii> Used for storing complex data structures, like a table.
iii) Example:
<?php
$students = [
[“John”, 25],
[“Alice”, 22]
];
Echo $students[0][0]; // Output: John
?>

2) Explain Bitwise operator with suitable example.


Ans:

3) State any four decisions making statements along with their syntax in PHP.
Ans: i) Decision-making statements in PHP help control the flow of execution based on
conditions.
ii) Here are four common decision-making statements with their syntax:
1. If Statement
a) Executes a block of code if the condition is true.
b) Syntax:
If (condition) {
// Code to execute if condition is true
}
c)Example:
<?php
$age = 18;
If ($age >= 18) {
Echo “You are eligible to vote.”;
}
?>

2. If-else Statement
a) Executes one block if the condition is true, otherwise executes another block.
b) Syntax:
If (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
c) Example:
<?php
$marks = 45;
If ($marks >= 50) {
Echo “Pass”;
} else {
Echo “Fail”;
}
?>

3. If-elseif-else Statement
a) Used when there are multiple conditions to check.
b) Syntax:
If (condition1) {
// Code if condition1 is true
} elseif (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
c) Example:
<?php
$score = 75;
If ($score >= 90) {
Echo “Grade A”;
} elseif ($score >= 75) {
Echo “Grade B”;
} else {
Echo “Grade C”;
}
?>

4. Switch Statement
a) Used to execute different blocks of code based on a variable’s value.
b) Syntax:
Switch (expression) {
Case value1:
// Code to execute if expression matches value1
Break;
Case value2:
// Code to execute if expression matches value2
Break;
Default:
// Code if no case matches
}
c) Example:
<?php
$day = “Monday”;
Switch ($day) {
Case “Monday”:
Echo “Start of the week!”;
Break;
Case “Friday”:
Echo “Weekend is near!”;
Break;
Default:
Echo “It’s a regular day.”;
}
?>

4) Write a PHP program to print factorial of number.


Ans:
<html>
<head><title>Factorial finder</title></head>
<body>
<?php
Function factorial($num) {
$fact = 1;
For ($i = $num; $i > 1; $i--) {
$fact *= $i;
}
Return $fact;
}

$number = 5;
Echo “Factorial of $number is: “ . factorial($number);
?>
</body>
</html>

You might also like