php (3)
php (3)
A. mysqli_query()
B. mysqli_connect()
C. mysql_connect()
D. mysqli_select_db()
Answer: B. mysqli_connect()
Answer: B. mysqli_query()
Answer: B. mysqli_close()
Answer: A. mysqli_select_db()
How do you prepare a SQL query to insert data into a MySQL database in PHP?
A. mysqli_query($conn, "INSERT INTO table (column) VALUES ('value')");
B. mysqli_prepare($conn, "INSERT INTO table (column) VALUES ('value')");
C. mysqli_exec($conn, "INSERT INTO table (column) VALUES ('value')");
D. mysqli_insert($conn, "INSERT INTO table (column) VALUES ('value')");
What is the correct way to retrieve data from a MySQL database in PHP?
A. mysqli_fetch_assoc()
B. mysqli_fetch_array()
C. mysqli_fetch_object()
D. All of the above
Which function is used to fetch the number of rows affected by a query in PHP MySQL?
A. mysqli_fetch_assoc()
B. mysqli_num_rows()
C. mysqli_affected_rows()
D. mysqli_fetch_row()
Answer: C. mysqli_affected_rows()
Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer
Answer: c
Explanation: The function array_count_values() will count all the values of an array. It will return an associative
array, where the keys will be the original array’s values, and the values are the number of occurrences.
(a) An integer
(b) A floating-point number
(c) An array
(d) A string
Explanation:
In PHP, the getdate() function returns an associative array containing information about the
current date and time.
Correct Answer: (d) Includes a file, and halts execution if the file is missing
Explanation:
require() includes the specified file, and if the file is not found, it produces a fatal error and
stops the script.
Explanation:
Superglobals are built-in PHP arrays that are always accessible, regardless of scope.
(a) $_POST
(b) $_GET
(c) $GLOBALS
(d) $SUPER
Explanation:
$SUPER does not exist. Common superglobals include $_GET, $_POST, $_SERVER, $_FILES,
$_COOKIE, $_SESSION, $_REQUEST, and $GLOBALS.
Which superglobal is used to access data sent via an HTML form using the POST method?
(a) $_GET
(b) $_POST
(c) $_FORM
(d) $_REQUEST
Correct Answer: (c) To access global variables from anywhere in the script
Explanation:
$GLOBALS is an associative array containing all global variables in the script, accessible from any
scope.
(a) $_FILES
(b) $_UPLOADS
(c) $_POST
(d) $_SERVER
Session:
A php session variable is used to store information about or change setting for a user . session variable hold
information about one single user and are available to all pages
$_SESSION[variable_name]=value_store
A. PHP sessions store data in the client-side browser using cookies only.
B. The session_start() function must be called before any HTML output.
C. Session variables are stored in the MySQL database by default.
D. session_destroy() deletes only a specific session variable.
Correct Answer: B
Explanation: session_start() must be called before any output is sent to the browser. It
initializes the session or resumes the current one based on a session identifier.
A. Browser cache
B. MySQL database
C. Server-side temporary files
D. HTML hidden fields
Correct Answer: C
Explanation: PHP stores session data on the server in temporary files unless configured
otherwise.
A. $_POST
B. $_SESSION
C. $_COOKIE
D. $_GLOBALS
Correct Answer: B
Explanation: $_SESSION is the superglobal array used to access session variables.
A. session_destroy()
B. unset()
C. session_unset()
D. session_remove()
Correct Answer: C
Explanation: session_unset() removes all session variables without destroying the session
itself.
Correct Answer: C
Explanation: session_destroy() deletes all session data, but does not unset the $_SESSION
array unless manually done.
A. A type of database
B. A server-side session variable
C. A small file stored on the user's computer
D. A function to connect to the server
Correct Answer: C
Explanation: A cookie is a small piece of data stored on the client (user's computer) by the
browser.
A. session_start()
B. set_cookie()
C. setcookie()
D. create_cookie()
Correct Answer: C
Explanation: setcookie() is the correct function to send a cookie from the server to the client.
A. $_SESSION
B. $_COOKIE
C. $_GET
D. $_SERVER
Correct Answer: B
Explanation: Cookie values can be retrieved using the $_COOKIE superglobal array.
Correct Answer: C
Explanation: setcookie() must be called before any output is sent to the browser, like
header().
A. /home/user/
B. /admin/
C. / (root)
D. The current script directory
Correct Answer: D
Explanation: If not specified, the cookie is only available within the directory of the script that
set it.
. Which of the looping statements is/are supported by PHP?
i) for loop
a) Only iv)
b) i) and ii)
c) i), ii) and iii)
d) i), ii), iii) and iv)
View Answer
Answer: d
Which variable is used to collect form data sent with both the GET and POST methods?
a) $_BOTH
b) $REQUEST
c) $_REQUEST
d) $BOTH
View Answer
Answer: c
Explanation: In PHP the global variable $_REQUEST is used to collect data after submitting an HTML form.
Which of the following PHP function will return true if a variable is an array or false if it is not an array?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer
Answer: b
1. <?php
2. $names = array("Sam", "Bob", "Jack");
3. echo $names[0] . "is the brother of " . $names[1] . " and " .
$names[1] . ".";
4. ?>
a) Sam is the brother of Bob and Jack.
b) Sam is the brother of Bob and Bob.
c) Sam is the brother of Jack and Bob.
d) Error
View Answer
Answer: b
Explanation: Simple definition of array and using it in a string. We have used $names[1] twice and hence Bob
appears twice.
Which function is responsible for sending a custom message to the system log?
a) systemlog()
b) syslog()
c) log_system()
d) sys_log()
View Answer
Answer: b
Php function:
Php function is a piece of code that can be reused many time it can take
input as argument list and return value
Code reusability
Less code
Easy to understand
<body>
<?php
function sayhello()
sayHello();
?>
function mytable($text)
print(“<td> $text</td>”);
print(“</tr></table>”);
1: sum.php
<html>
<head>
</head>
</form>
</body>
</html>
2. sum1.php
<?php
$a=$_POST[“fname”];
$b=$_POST[“lname”];
$c=$a+$b;
?>
Ans c)
Ans b)
Ans c)
Ans c)
Ans a)
. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 1
b) 5
c) 12
d) Error
View Answer
Answer: b
Which of the functions is used to sort an array in descending order?
a) sort()
b) asort()
c) rsort()
d) dsort()
View Answer
Answer: c
Explanation: The function sort() will sort the arrays in ascending order, the function rsort() will sort arrays in
descending order. While the function asort() will sort associative arrays in ascending order, according to the
value.
Explanation:
Explanation:
. What is the difference between Indexed array and Associative array in PHP?
A. Index array has numeric index while associative array has named keys
B. Index array has numeric index while associative array has named keys and numeric index
both
C. Index array is one-dimensional array with numeric index while associative array is two-
dimensional array with numeric index
D. Index array has numeric index while associative array has one or more arrays
Answer: A) Index array has numeric index while associative array has named keys
Explanation:
The main difference between Indexed array and Associative Array is that - Index array has numeric
index while associative array has named keys.
Explanation:
Explanation:
Which PHP function(s) is/are used to compare arrays and returns the differences?
A. array_diff()
B. array_diff_assoc()
C. array_diff_key()
D. All of the above
Explanation:
All of the above PHP functions are used to compare arrays and returns the differences.
A. array_diff() compares the values only while array_diff_assoc() compares the keys only
B. array_diff() compares the values only while array_diff_assoc() compares the keys and values
C. Both functions can be used to compare the values and keys
D. None of the above
Answer: B) array_diff() compares the values only while array_diff_assoc() compares the keys and
values
Explanation:
A. array_sort()
B. sort()
C. multisort()
D. array_multisort()
Answer: D) array_multisort()
Explanation:
Explanation:
A. sort_reverse()
B. reverse_sort()
C. revsort()
D. rsort()
Answer: D) rsort()
Explanation:
The PHP function rsort() is used to sort an indexed array in descending order.
A. get()
B. start()
C. current()
D. cur()
Answer: C) current()
Explanation:
The PHP function current() is used to return the current element in an array.
A. in_array()
B. check_array()
C. exist()
D. None of the above
Answer: A) in_array()
Explanation:
The PHP function in_array() is used to check if a specified value exists in an array.
Which PHP function is used to get one or more random keys from an array?
A. array_random()
B. array_randomize()
C. array_rand()
D. krand()
Answer: C) array_rand()
Explanation:
The PHP function array_rand() is used to get one or more random keys from an array.
Answer: B) To access the information about headers, paths, and script locations
Explanation:
The PHP $_SERVER is used to get the information about headers, paths, and script locations.
Which PHP global variable is used to collect form data after submitting an HTML form with
method="post"?
A. $_GET
B. $_REQUEST
C. $_POST
D. $_ENV
Answer: C) $_POST
Php string:
Php string is a sequence of character used to store and manipulate text PHP
Single quoted
<?php
$str=’hello’;
Echo $str;
?>
<?php
$str=”hello”;
Echo $str;
?>
A. strlen()
B. count()
C. strcount()
D. length()
Answer: A. strlen()
A. strpos()
B. substr()
C. str_find()
D. str_search()
Answer: A. strpos()
A. bcd
B. abc
C. cde
D. def
Answer: A. bcd
A. implode()
B. split()
C. explode()
D. divide()
Answer: C. explode()
58) The … function capitalizes the first letter of the string str if it is alphabetical.
A. strcap()
B. firstcap()
C. ucfirst()
D. ucasefirst()
Ans c)
59) Which function is used to convert all characters of a string to lowercase in PHP?
A. strlower()
B. strtolower()
C. lc()
D. lowercase()
Answer: B. strtolower()
A. strcut()
B. substr()
C. strsub()
D. partstr()
Answer: B. substr()
61) The … function returns the number of times one string occurs within another.
A. strstr()
B. strtr()
C. substr()
D. substr_count()
Ans d)
ans b)
63) Which of the following is the correct way to declare a string in PHP?
a) $string = 'Hello World';
c) Both a) and b)
ans c)
64) What is the output of the following PHP code? $str = "Hello"; echo $str[1];
a) H
b) e
c) l
d) o
ans b)
65) How do you replace 'world' with 'PHP' in the string 'Hello world' using PHP?
a) replace('world', 'PHP', 'Hello world');
Answer:
66) Which of the following is not a valid way to declare a multiline string in PHP?
a) Using single quotes
Answer:
Explanation:
Multiline strings can be declared using double quotes, heredoc, or nowdoc syntax, but not with
single quotes.
a) if –else
b) switch
if statement : a block of code must test a specific condition if condition is true the the code inside the
true block execute otherwise false block
if(condition)
True block
Else
False block
}
Switch : php switch statement is used to execute one statement from multiple condition it work like php
If--- else
Switch(expr)
Case 1:
Block of statement;
Case 2:
Block of statement;
Default:
Looping:
Php for loop can be used to traverse set of code for the specified number of time
For($n=1;$n<=10; $n++)
Echo “$n”;
While loop: php while loop can be used to traverse set of code it is entry control loop
While($n<=10)
Echo “$n”;
$n++;
Body of loop
}while(condition);
n=1;
do
Echo “$n”;
$n++;
}while($n<=10);
Php for each loop : php for each loop is used to traverse array element
Foreach($array as $var)
{
}
jump statement:
Php break: php break statement break the execution of the current for loop
For($i=1;$i<=10;$i++)
Echo “$i”;
If($i==5)
Break;
}
}
It continue the current flow of the program and skip the remaining code at the specified condition
For($i=1;$i<=10;$i++)
{
If(i==5)
Continue;
Echo “$i”;
1. <?php
2. $x;
3. if ($x)
4. print "hi" ;
5. else
6. print "how are u";
7. ?>
a) how are u
b) hi
c) error
d) no output
1. <?php
2. $a = 1;
3. if (echo $a)
4. print "True";
5. else
6. print "False";
7. ?>
a) true
b) false
c) error
d) no output
1. <?php
2. $a = "1";
3. switch ($a)
4. {
5. case 1:
6. print "hi";
7. case 2:
8. print "hello";
9. default:
10. print "hi1";
11. }
12. ?>
a) hihellohi1
b) hi
c) hihi1
d) hi1
1. <?php
2. $a = "2";
3. switch ($a)
4. {
5. case 1:
6. print "hi";
7. case 2:
8. print "hello";
9. break;
10. default:
11. print "hi1";
12. }
13. ?>
a) hihellohi1
b) hello
c) hihi1
d) hi1
View Answer
Answer: b
1. <?php
2. while()
3. {
4. print "hi";
5. }
6. ?>
a) infinite loop
b) hi
c) no output
d) error
View Answer
Answer: d
Explanation: The while loop cannot be defined without a condition.
1. <?php
2. do
3. {
4. print "hi";
5. }
6. while(0);
7. print "hello";
8. ?>
a) infinite loop
b) hihello
c) hello
d) error
View Answer
Answer: b
Explanation: The do while loop executes at least once as the condition is in the while loop.
1. <?php
2. $i = 0
3. while ($i < 3)
4. {
5. $i++;
6. }
7. print $i;
8. ?>
a) 2
b) 3
c) 0
d) 1
View Answer
Answer: b
Explanation: The increment happens and then the check happens.
1)What is php
a) Php is an open source b) php is used to develop dynamic and interactive web page
b) Php is a server side scripting language d) all of the mention
Ans: d)
2)Who is father of PHP
5) Which of the following is the correct way to add a comment in PHP codes
8) what is the name of an array that stores all global variable in PHP
a) input constant b) user defined constant c) Magic constant d) default constant ans c)
A) MY_CONSTANT
B) MYconstant
C) myConstant
D) 2CONSTANT
Answer: D) 2CONSTANT
19) Which of the following PHP variable types is not a valid type in PHP?
A) $int
B) $string
C) $boolean
D) $float
A) Variables
B) Constants
C) Both variables and constants
D) Neither variables nor constants
Answer: A) Variables
21) Which of the following is the correct way to define a constant in PHP?
A) const PI = 3.14;
B) constant PI = 3.14;
C) define("PI", 3.14);
D) Both A and C
23) Which of the following PHP superglobals are used to access form data sent via POST
method?
A) $_GET
B) $_POST
C) $_SERVER
D) $_COOKIE
Answer: B) $_POST
24) What will happen if you try to modify the value of a constant in PHP?
a) rendering problem
C) parsing error
d)none
Ans c)
26) which function is used for determining the location of syntax error
a) error
b)syn_error
c)die
d) find()
Ans b)
27) in multidimensional array rather than a single key the values are stored in
b)2keys
c) linear style
d) none
Ans b)
In a multidimensional array in PHP (or other programming languages), values are stored using two (or
more) keys. The first key typically references the outer array, and the second key is used to access
elements within the inner array.
a) is_array
b) sizeof()
c)array_count()
d) count()
Ans d)
a) name
b) location
c) index
d) none
ans c)
a) constants may be define and accessed anywhere without regard to variable scoping rules
b) once the constant have been set may not be redefined or undeined
c) both
d) none
ans c)
a) "Constants may be defined and accessed anywhere without regard to variable scoping
rules"
True – Constants in PHP are globally accessible once defined, regardless of scope.
b) "Once the constants have been set, they may not be redefined or undefined"
True – After a constant is defined using define(), it cannot be changed or removed during
the script execution.
a)javascript
b) PERL and C
c) Visual Basic
d)VBScript
ans b)
PHP's syntax is heavily influenced by C and Perl, especially in terms of:
<?php
echo strpos("Hello, Includehelp!", "Includehelp!");
?>
A. 6
B. 7
C. 8
D. -1
Answer: B) 7
A. str_replace()
B. replace()
C. replace_str()
D. string-replace()
Answer: A) str_replace()
34) Which function is used to convert the ASCII value to the character?
A. asc()
B. str()
C. char()
D. chr()
Answer: D) chr()
35) Which PHP function is used to get the length of the string?
A. strlength()
B. strlen()
C. length()
D. str_len()
Answer: C) length()
36) Which PHP functions are used to convert string to lowercase and uppercase?
Explanation:
The strtoupper() function is used to convert the string to uppercase. And, strtolower() function
is used to convert the string to lowercase.
A. not-a-number
B. not-a-numerical
C. nothing-a-number
D. numeric-a-number
Answer: A) not-a-number
A. random()
B. randomize()
C. rand()
D. randnumbers()
Answer: C) rand()
A. Local
B. Global
C. Static
D. Fixed
Answer: B) Global
Index array:
Associative array: we can associate name with each array element in php
$salary=array(“sonoo”=>”35000”, “john”=>”450000”);
echo “salary[“sonoo”]”;
40) Which of the following is the correct way to create an indexed array in PHP?
A) $arr = "Apple", "Banana", "Mango";
B) $arr = array("Apple", "Banana", "Mango");
C) $arr = array{"Apple", "Banana", "Mango"};
D) $arr = "Apple" => 0, "Banana" => 1;
Correct Answer: B
41) How can you access the value "Banana" from this array: $fruits = array("Apple",
"Banana", "Mango");
A) $fruits[2];
B) $fruits["Banana"];
C) $fruits[1];
D) $fruits->1;
Correct Answer: C
<?php
$temp=array(
array(1,”sono”,40000),
array(2,”sono”,40000)
);
for($row=0;$row<2;$row++)
for($col=0;$col<3;$col++)
{ echo $temp[$row][$col];
Echo “<br/>”;
?>
44) How do you access the value "dog" in the following array?
$animals = [
["cat", "dog"],
["cow", "goat"]
];
A) $animals[0][1]
B) $animals[1][1]
C) $animals[1]["dog"]
D) $animals[0]["dog"]
Correct Answer: A
foreach($array as $var)