0% found this document useful (0 votes)
17 views20 pages

WBP Question Bank(CT-I) solved

The document is a comprehensive question bank on PHP, covering various topics including advantages of PHP, data types, string functions, loops, arrays, and user-defined functions. It provides explanations, syntax, and examples for each concept, making it a useful resource for learning PHP programming. Additionally, it discusses specific functions like implode, explode, and bitwise operators, as well as practical programming tasks such as checking even/odd numbers and calculating factorials.

Uploaded by

ARYAN MOHADE
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)
17 views20 pages

WBP Question Bank(CT-I) solved

The document is a comprehensive question bank on PHP, covering various topics including advantages of PHP, data types, string functions, loops, arrays, and user-defined functions. It provides explanations, syntax, and examples for each concept, making it a useful resource for learning PHP programming. Additionally, it discusses specific functions like implode, explode, and bitwise operators, as well as practical programming tasks such as checking even/odd numbers and calculating factorials.

Uploaded by

ARYAN MOHADE
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/ 20

WBP Question Bank(CT-I)

1.List advantages of PHP

❖​ Easy to Learn
❖​ Familiarity with Syntax
❖​ PHP is an open-source web development language, it‟s completely free of
cost.
❖​ PHP is one of the best user-friendly programming languages in the
industry.
❖​ PHP supports all of the leading databases, including MySQL, ODBC,
SQLite and more effective and efficient programming language
❖​ Platform Independent

2.Explain any four string functions with use.


3 state different data types in php.

Four Data Types in PHP

❖​ Boolean: Represents two possible values: true or false.


❖​ Integer: A whole number (positive or negative) without decimals.
❖​ Float: A number with a decimal point or in exponential form.
❖​ String: A sequence of characters enclosed in single or double quotes.
❖​ Array: A collection of values stored in a single variable.
❖​ Object: An instance of a class containing data and methods.
❖​ Resource: A special type used to reference external resources like file
handles.
❖​ NULL: Represents a variable with no value assigned.

4 Explain array-flip() and explode() function with syntax

1) array_flip() Function

1.​ The array_flip() function swaps the keys and values of an array.
2.​ If a value occurs more than once, the last key becomes its value.
3.​ It is used to reverse the mapping of keys and values in an associative
array.
4.​ Only works with values that are strings or integers.

Syntax:

array_flip(array $array): array

Example:

<?php
$arr = array("a" => 1, "b" => 2, "c" => 3);
print_r(array_flip($arr));
?>

Output:

Array ( [1] => a [2] => b [3] => c )

2) explode() Function

1.​ The explode() function splits a string into an array based on a delimiter.
2.​ It is useful for breaking a string into smaller parts, like splitting words.
3.​ The delimiter specifies where the string should be split.
4.​ The result is an array containing the string segments.

Syntax:

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Example:

<?php
$str = "Hello,World,PHP";
$arr = explode(",", $str);
print_r($arr);
?>

Output:

Array ( [0] => Hello [1] => World [2] => PHP )

5.Explain use of for and foreach with an example.

For Loop in PHP

1.​ The for loop is used to execute a block of code a specific number of
times.
2.​ It is ideal when the number of iterations is known beforehand.
3.​ The loop consists of three parts: initialization, condition, and
increment/decrement.
4.​ The code inside the loop executes as long as the condition is true.
5.​ It is commonly used for numerical tasks or counting.

Syntax:

for (initialization; condition; increment/decrement) {


// Code to execute

Example (For Loop):

<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i<br>";
}
?>

Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

Foreach Loop in PHP

1.​ The foreach loop is used to iterate through arrays.


2.​ It executes once for each element in the array.
3.​ Works for both indexed and associative arrays.
4.​ It is easier to use compared to a for loop for arrays.
5.​ Useful when you don't need to keep track of the array index.

Syntax:

foreach ($array as $value) {

// Code to execute

}
foreach ($array as $key => $value) {

// Code to execute

Example (Foreach Loop):

<?php
$fruits = array("Apple", "Banana", "Cherry");
foreach ($fruits as $fruit) {
echo "Fruit: $fruit<br>";
}
?>

Output:

Fruit: Apple

Fruit: Banana

Fruit: Cherry

6.Explain bitwise operators in php.

Bitwise Operators in PHP

1.​ Bitwise operators perform operations on the binary representations of


numbers.
2.​ These operators are used for tasks like setting, clearing, and toggling bits.
3.​ Common operators include AND (&), OR (|), XOR (^), NOT (~), and bit
shifts (<<, >>).
4.​ The operands are treated as integers, and operations are performed at the
bit level.
5.​ Bitwise operators are commonly used in low-level programming or
optimization tasks.
Syntax:

$result = $a & $b; // Bitwise AND

$result = $a | $b; // Bitwise OR

$result = $a ^ $b; // Bitwise XOR

$result = ~$a; // Bitwise NOT

$result = $a << 1; // Left Shift

$result = $a >> 1; // Right Shift

Example:

<?php
$a = 6; // Binary: 110
$b = 3; // Binary: 011

echo $a & $b; // Output: 2 (Binary: 010 - Bitwise AND)


echo "<br>";
echo $a | $b; // Output: 7 (Binary: 111 - Bitwise OR)
echo "<br>";
echo $a ^ $b; // Output: 5 (Binary: 101 - Bitwise XOR)
?>

Output:

5
7.Explain how to implement a multidimensional array in php.

Implementing a Multidimensional Array in PHP

1.​ A multidimensional array is an array containing one or more arrays as its


elements.
2.​ It allows storing data in a matrix or table-like structure.
3.​ Elements are accessed using multiple indices (e.g.,
$array[row][column]).
4.​ Commonly used for complex data storage, such as representing grids or
records.
5.​ Can be created manually or dynamically during runtime.

Syntax:

$array = array(

array(value1, value2),

array(value3, value4)

);

Example:

<?php
$students = array(
array("Aryan", 19, "IT"),
array("Raj", 20, "CS"),
array("Sara", 18, "EC")
);

echo "Name: " . $students[0][0] . ", Age: " .


$students[0][1] . ", Branch: " . $students[0][2];
// Output: Name: Aryan, Age: 19, Branch: IT
?>

8.Explain print and echo statements in php.

Print Statement in PHP:

1.​ Print is a language construct used to display output.


2.​ It returns a value of 1, so it can be used in expressions.
3.​ Print can output only one string at a time.
4.​ It is slightly slower than echo.
5.​ Parentheses are optional while using print.

Example (Print):

<?php
print "Hello, World!<br>";
$result = print "Learning PHP"; // Returns 1
echo "<br>Result: $result";
?>

Output:

Hello, World!

Learning PHP

Result: 1
Echo Statement in PHP:

1.​ Echo is a language construct used to display output.


2.​ It does not return any value (faster than print).
3.​ Echo can output multiple strings separated by commas.
4.​ Commonly used for displaying text and variables.
5.​ Parentheses are not allowed when using multiple arguments in echo.

Example (Echo):

<?php
echo "Hello", ", ", "World!<br>";
$name = "PHP";
echo "Learning ", $name, " is fun!";
?>
Output:

Hello, World!

Learning PHP is fun!

9.Explain the following function types with example

1)Anonymous function 2)Variable functions

1) Anonymous Function

1.​ An anonymous function is a function without a name.


2.​ It is often used as a value for variables or as a callback.
3.​ These functions are defined using the function keyword.
4.​ They provide flexibility for one-time use or inline functionality.

Syntax:
$variable = function() {

// Function body

};

Example:

<?php
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("Aryan"); // Output: Hello, Aryan!
?>

2) Variable Functions

1.​ Variable functions allow calling a function using a variable name.


2.​ The variable contains the name of the function to call.
3.​ It is useful for dynamic function invocation.
4.​ The variable must reference a valid function name.

Syntax:

functionName(); // Called using a variable

Example:

<?php
function sayHello() {
return "Hello, World!";
}
$func = "sayHello";
echo $func(); // Output: Hello, World!
?>

10.State the use of “$” sign in PHP

Explanation:

The $ sign in PHP is used to define a variable, which stores data like numbers,
strings, etc.

●​ A variable name must start with a letter or underscore (_) and cannot
contain spaces.

Example:
<?php
$a = 10; // Assign 10 to variable $a
$b = 20; // Assign 20 to variable $b
$sum = $a + $b; // Add $a and $b
echo "Sum is: $sum"; // Output the result
?>

Output:

Sum is: 30

11.Write a program using do-while loop

<?php
// Initialize the counter variable
$counter = 1;
// Start of the do-while loop
do {
// Print the current value of the counter
echo "The number is: $counter<br>";
// Increment the counter by 1
$counter++;
} while ($counter <= 5); // Condition to repeat the loop
until counter is less than or equal to 5
// End of program
?>

12 Explain associative and multidimensional arrays.

Associative Array

1.​ An associative array uses named keys instead of numeric indices.


2.​ Keys are used to access specific values in the array.
3.​ It is commonly used to store key-value pairs.
4.​ Keys can be strings, making it easier to understand and manage data.
5.​ Used when data needs meaningful identifiers for each value.

Syntax:

$array = array("key1" => "value1", "key2" => "value2");

Example:

<?php
$student = array("name" => "Aryan", "age" => 19, "branch" =>
"IT");
echo "Name: " . $student["name"] . ", Age: " .
$student["age"] . ", Branch: " . $student["branch"];
// Output: Name: Aryan, Age: 19, Branch: IT
?>
Multidimensional Array

1.​ A multidimensional array is an array containing one or more arrays.


2.​ It allows organizing data in rows and columns, similar to a matrix.
3.​ Elements are accessed using multiple indices (e.g.,
$array[row][column]).
4.​ It is useful for storing complex data structures like records or grids.
5.​ Nested arrays can have indexed or associative formats.

Syntax:

$array = array(

array("value1", "value2"),

array("value3", "value4")

);

Example:

<?php
$students = array(
array("name" => "Aryan", "age" => 19, "branch" => "IT"),
array("name" => "Raj", "age" => 20, "branch" => "CS")
);
echo "Name: " . $students[0]["name"] . ", Age: " .
$students[0]["age"] . ", Branch: " . $students[0]["branch"];
// Output: Name: Aryan, Age: 19, Branch: IT
?>

13.Differentiate between implode and explode functions.


Implode Function Explode Function

The implode function works on an The explode function works on a


array. string.

The implode function returns a string. The explode function returns an array.

The first parameter of the implode The first parameter of the explode
function is optional. function is required.

Joins array elements in the order they Splits a string into elements based on
appear. the separator.

Handles only arrays; passing Works only with strings; non-string


non-array inputs throws an error. input may cause unexpected results.

string implode(string $separator, array array explode(string $separator, string


$array) $string)

<?php <?php
$arr = array('CO', 'IF', 'EJ'); $str = "CO-IF-EJ";
$str = implode("-", $arr); $arr = explode("-", $str);
echo $str; // Output: CO-IF-EJ print_r($arr); // Output: Array ( [0] =>
?> CO [1] => IF [2] => EJ )
?>

14.State user defined function and explain it with an example.

User-Defined Function in PHP

1.​ A user-defined function is a custom function created by the user to perform


a specific task.
2.​ Functions help organize code, avoid repetition, and improve readability.
3.​ A function is defined using the function keyword followed by its name.
4.​ Functions can take parameters as input and return a value using the
return statement.
5.​ Functions are executed only when they are called.
Syntax:

function functionName($parameter1, $parameter2) {

// Code to execute

return $result; // Optional

Example:

<?php
function addNumbers($a, $b) {
return $a + $b; // Add two numbers and return the result
}
echo "Sum: " . addNumbers(5, 10); // Call the function
?>

Output:

Sum: 15

15 List different types of arrays.

Types of Arrays in PHP:

1.​ Indexed Array:​

○​ Stores elements with numeric indices starting from 0.


2.​ Associative Array:​

○​ Stores elements with named keys instead of numeric indices.


3.​ Multidimensional Array:​

○​ Contains one or more arrays within it, allowing for complex data
structures.

16.Write a PHP program to check whether a number is even or odd using a


function.

<?php
// Function to check if a number is even or odd
function checkEvenOdd($number) {
// Use modulus operator to determine even or odd
if ($number % 2 == 0) {
return "$number is Even.";
} else {
return "$number is Odd.";
}
}
// Input number
$number = 7; // Change this number to test different cases
// Call the function and display the result
echo checkEvenOdd($number);
?>
output
7 is Odd.

17.Write PHP program to calculate the sum of digits using functions.

<?php
// Function to calculate the sum of digits of a number
function sumOfDigits($number) {
$sum = 0; // Initialize sum to 0
// Loop through each digit of the number
while ($number > 0) {
$sum += $number % 10; // Add the last digit to the
sum
$number = (int)($number / 10); // Remove the last
digit from the number
}
return $sum; // Return the calculated sum
}
// Input number (example: 12345)
$number = 12345;
// Calculate the sum of digits by calling the function
$result = sumOfDigits($number);
// Output the result
echo "The sum of the digits of $number is: $result\n";
?>

18.Write PHP program to print factorial of number.

Ans.
<?php
// Function to calculate factorial
function factorial($num) {
// Initialize the factorial to 1
$factorial = 1;
// Loop from 1 to the given number
for ($i = 1; $i <= $num; $i++) {
$factorial *= $i; // Multiply each number with the
current factorial
}
// Return the calculated factorial
return $factorial;
}
// Input number
$number = 5; // You can change this number to test for other
values
// Calculate and display the factorial
echo "The factorial of $number is: " . factorial($number);
?>
Output
The factorial of 5 is: 120

19.Why PHP is called a loosely typed language.Explain with example.

❖​ PHP does not require explicit declaration of data types for variables.
❖​ Variables can hold values of any type and change their type dynamically.
❖​ The type of a variable is determined automatically based on the value
assigned.
❖​ This flexibility makes PHP easier to use but requires careful handling of
data.
❖​ Type conversion (implicit or explicit) happens automatically during
operations.
❖​ Loosely typed nature allows mixing of different data types in operations.

<?php
$var = 10; // Initially an integer
echo "Integer value: $var<br>";
$var = "Hello"; // Now a string
echo "String value: $var<br>";
$var = $var + 5; // Implicit conversion: "Hello" treated as
0
echo "After addition: $var"; // Output: 5
?>
Integer value: 10
String value: Hello
After addition: 5

20state the difference between $ and $$ in php with an example.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy