WBP Question Bank(CT-I) solved
WBP Question Bank(CT-I) solved
❖ 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
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:
Example:
<?php
$arr = array("a" => 1, "b" => 2, "c" => 3);
print_r(array_flip($arr));
?>
Output:
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:
Example:
<?php
$str = "Hello,World,PHP";
$arr = explode(",", $str);
print_r($arr);
?>
Output:
Array ( [0] => Hello [1] => World [2] => 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:
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i<br>";
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Syntax:
// Code to execute
}
foreach ($array as $key => $value) {
// Code to execute
<?php
$fruits = array("Apple", "Banana", "Cherry");
foreach ($fruits as $fruit) {
echo "Fruit: $fruit<br>";
}
?>
Output:
Fruit: Apple
Fruit: Banana
Fruit: Cherry
Example:
<?php
$a = 6; // Binary: 110
$b = 3; // Binary: 011
Output:
5
7.Explain how to implement a multidimensional array in php.
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")
);
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:
Example (Echo):
<?php
echo "Hello", ", ", "World!<br>";
$name = "PHP";
echo "Learning ", $name, " is fun!";
?>
Output:
Hello, World!
1) Anonymous Function
Syntax:
$variable = function() {
// Function body
};
Example:
<?php
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("Aryan"); // Output: Hello, Aryan!
?>
2) Variable Functions
Syntax:
Example:
<?php
function sayHello() {
return "Hello, World!";
}
$func = "sayHello";
echo $func(); // Output: Hello, World!
?>
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
<?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
?>
Associative Array
Syntax:
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
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
?>
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.
<?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 )
?>
// Code to execute
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
○ Contains one or more arrays within it, allowing for complex data
structures.
<?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.
<?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";
?>
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
❖ 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