Question Bank Final PHP
Question Bank Final PHP
ENGINEERING AND
TECHNOLOGY (DIPLOMA
STUDIES)
COMPUTER ENGG.
DEPARTMENT
4th SEMESTER
Web Development Using PHP (03606265)
Question Bank
Academic Year : 2024-25
Web Development Using PHP-(03606265 )
QUESTION BANK
Example:
Output:
Hellohowareyou
I am Fine;
5. What are the data types in PHP? Provide an example of any two.
Answer: -
Datatypes:
1. String
2. Integer
3. Float
4. Boolean
5. Array
6. Object
7. NULL
8. Resource
Example:
1) Integer
<?php
$num1 = 10;
$num2 = 5;
$sum = $num1 + $num2; // Sum of two integers
echo "The sum is: " . $sum; // Outputs: The sum is: 15
?>
2) String
<?php
// Define a string variable
$message = "Hello, World!";
// Print the string
echo $message; // Outputs: Hello, World!
?>
1) Arithmetic operators
2) Assignment operators
3) Comparison operators
4) Increment/Decrement operators
5) Logical operators
6) Ternary operator
1. Arithmetic Operators
9. What is the syntax of a for loop in PHP? Write a for loop to print
numbers from 1 to 5.
Answer: -
PHP for loop can be used to traverse set of code for the specified
number of times.
It should be used if the number of iterations is known otherwise use
while loop. This means for loop is used when you already know how
many times you want to execute a block of code.
Syntax:
for (initialization; condition; increment/decrement){
//code to be executed
}
initialization - Initialize the loop counter value. The initial value of the for
loop is done only once. This parameter is optional.
condition - Evaluate each iteration value. The loop continuously executes
until the condition is false. If TRUE, the loop execution continues,
otherwise the execution of the loop ends.
Increment/decrement - It increments or decrements the value of the
variable.
To Print 1 to 5 numbers.
<?php
for($n=1;$n<=5;$n++){
echo "$n<br/>";
}
?>
Output:
1
2
3
4
5
10. What is the difference between a while loop and a do-while loop in
PHP?
Answer: -
e.g:
e.g:
<?php
$n=1; <?php
while($n<=5){ $n=1;
echo "$n<br/>"; do{
$n++; echo "$n<br/>";
} $n++;
?> }while($n<=5);
?>
2) Continue statement
Example:
<?php
//outer loop
for ($i =1; $i<=3; $i++) {
//inner loop
for ($j=1; $j<=3; $j++) {
if (!($i == $j) ) {
continue; //skip when i and j does not have same values
}
echo $i.$j;
echo "</br>";
}
}
?>
Output:
11
22
33
3) break statement
13. What is the difference between Call by Value and Call by Reference
in PHP? Explain with an example of each.
Answer: -
1) Call by Value
Example:
<?php
function add($num) {
$original = 10;
add($original); // Call by Value
echo "Outside Function: $original\n"; // Outputs: 10
?>
2) Call by Reference
<?php
function add(&$num) {
$original = 10;
?>
In this method, the value of each In this method, the address of actual
variable in the calling function is variables in the calling function is
copied into corresponding dummy copied into the dummy variables of the
variables of the called function. called function.
15. What is the purpose of the strlen() function in PHP? How would you
use it to find the length of a string?
Answer: -
The strlen() function in PHP is used to calculate the length of a string, i.e.,
it returns the number of characters present in the string. This includes all
characters, spaces, and special characters.
Syntax:
strlen(string);
Parameter: The function takes a single parameter, which is the string
whose length you want to find.
Return Value: It returns an integer representing the number of characters
in the string.
Example:
<?php
$string = "Hello, PHP!";
$length = strlen($string);
echo "The length of the string is: $length";
?>
Outputs:
The length of the string is: 12
16. Explain the strcmp() function in PHP. How would you compare two
strings for equality or ordering? Provide an example.
Answer: -
The strcmp() function in PHP is used to compare two strings. It performs
a case-sensitive comparison, meaning that uppercase and lowercase letters
are considered different. The function returns an integer value based on the
comparison:
Returns 0: If the two strings are identical.
Returns a value less than 0: If the first string is less than the second string.
Returns a value greater than 0: If the first string is greater than the second
string.
Syntax:
strcmp(string1, string2);
To compare two strings:
Use strcmp() to check if two strings are equal or if one is greater than
the other.
The result can be used in conditional statements or for sorting.
Example:
<?php
$string1 = "Hello";
$string2 = "hello";
if ($string1 == $string2) {
echo "The strings are equal."; // This will NOT print.
} else {
echo "The strings are not equal."; // This will print.
}
?>
17. How do you reverse a string in PHP? What function do you use, and
how does it work?
Answer: -
In PHP, you can reverse a string using the built-in function strrev(). This
function takes a string as input and returns the string with its characters in
reverse order.
Syntax:
strrev(string);
Example:
<?php
$string = "Hello, PHP!";
$reversedString = strrev($string);
echo $reversedString;
?>
Outputs:
!PHP ,olleH
CHAPTER:4 PHP Arrays and Handling Html Form with Php
18. What are the different types of arrays in PHP? Explain the difference
between indexed arrays, associative arrays with examples.
Answer: -
In PHP, an array is a data structure that allows you to store multiple values
in a single variable.
e.g:
e.g:
$assocArray = array("name" =>
$indexedArray=array("Apple", "John", "age" => 25, "city" => "New
"Banana", "Orange"); York");
19. Explain the significance of the Array identifier and how it is used in
PHP. Provide examples demonstrating its usage.
Answer: -
In PHP, the $ symbol is used to indicate variables. The array identifier, $,
followed by the array variable name is used to access or manipulate array
elements.
Answer: -
Associative Arrays in PHP are arrays where each element is associated
with a specific key rather than a numeric index.
Here's an example:
In this example, the keys "name," "age," and "city" are associated with
corresponding values, forming an associative array. This allows for more
descriptive and meaningful indexing compared to traditional indexed
arrays.
21. How can you define a starting index value for an array in PHP?
Provide a code example.
Answer: -
In PHP, define a starting index value for an array by explicitly assigning
values to specific indices.
Here's an example:
Cookies Session
Cookies end on the lifetime set When the user quits the browser or logs out
by the user. of the programmed, the session is over.
Cookies stored data in text file. Session save data in encrypted form.
23. Explain the setcookie() function with its parameters and how it works
in PHP.
Answer: -
The setcookie() function in PHP is used to send a cookie from the server
to the client's browser. Cookies are small pieces of data that are stored on
the user's computer and can be used to maintain user-specific information
across multiple pages or sessions.
Syntax of setcookie():
setcookie(name, value, expire, path, domain, secure, httponly);
Parameters of setcookie()
1. name (Required):
o The name of the cookie. This is the identifier used to retrieve the
cookie's value on the client side.
o Example: "username"
2. value (Optional):
o The value associated with the cookie. This is the data you want to
store in the cookie.
o Example: "JohnDoe"
o If this parameter is not set, the cookie will be deleted.
3. expire (Optional):
o The expiration time of the cookie, specified as a timestamp (a Unix
timestamp). It defines when the cookie will expire.
o A value of 0 or a negative number will make the cookie a session
cookie, which expires when the browser is closed.
o Example: time() + 3600 (cookie expires in 1 hour from the current
time).
o Example: time() + 3600 * 24 * 30 (cookie expires in 30 days).
4. path (Optional):
o The path on the server where the cookie will be available. The
default value is /, meaning the cookie will be available throughout
the entire domain.
o Example: / (available to the whole domain), /folder/ (only available
within /folder/).
5. domain (Optional):
o The domain that the cookie is available to. This defines which
domains can access the cookie. The default is the domain of the
calling script.
o Example: "example.com" (the cookie will be available to this
domain and subdomains).
6. secure (Optional):
o A boolean value that indicates whether the cookie should only be
sent over a secure HTTPS connection.
o Set it to true if the cookie should only be sent over HTTPS.
o Example: true (only sent over HTTPS) or false (sent over HTTP as
well).
7. httponly (Optional):
o A boolean value that specifies whether the cookie should be
accessible via JavaScript. If set to true, the cookie is not accessible
via JavaScript (helps mitigate the risk of cross-site scripting attacks).
o Example: true (not accessible via JavaScript) or false (accessible via
JavaScript).
<?php
session_start();
$_SESSION["user_id"] = 12345;
?>
Sessions are more secure because they store data on the server, making
them suitable for sensitive information that should not be exposed to
the client.
Cookies are stored on the client side, making them more appropriate for
non-sensitive, persistent data that needs to be stored on the user's
device.
Example:
<?php
session_start();
$_SESSION["username"] = "JohnDoe";
$_SESSION["email"] = "[email protected]";
?>
CHAPTER:6 PHP Database
27. Which functions are used to integrate php with MySQL.
Answer: -
It is possible to execute various commands of MySQL from PHP
application. PHP provides various built in functions that allow you to use
MySQL commands from PHP page. Thus you can integrate PHP with.
MySQL.
Following are the various functions of PHP that allows you the facility of
integrating PHP with. MySQL.
1 mysqli_connect()
2 mysqli_select db()
3 mysqli_query()
4 mysqli_fetch_row()
5 mysqli_fetch_array()
6 mysqli_error()
7 mysqli_num_rows()
8 mysqli_close()