0% found this document useful (0 votes)
5 views

php (3)

The document provides a comprehensive overview of PHP functions and their usage, particularly in relation to MySQL database operations, session management, and cookie handling. It includes multiple-choice questions and answers, explaining functions like mysqli_connect(), mysqli_query(), and setcookie(), as well as concepts like superglobals and session variables. Additionally, it covers array types, error handling, and function syntax in PHP.
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)
5 views

php (3)

The document provides a comprehensive overview of PHP functions and their usage, particularly in relation to MySQL database operations, session management, and cookie handling. It includes multiple-choice questions and answers, explaining functions like mysqli_connect(), mysqli_query(), and setcookie(), as well as concepts like superglobals and session variables. Additionally, it covers array types, error handling, and function syntax in PHP.
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/ 42

Which PHP function is used to establish a connection to a MySQL database?

A. mysqli_query()
B. mysqli_connect()
C. mysql_connect()
D. mysqli_select_db()

Answer: B. mysqli_connect()

Which PHP function is used to execute a MySQL query?


A. mysqli_fetch_array()
B. mysqli_query()
C. mysqli_connect()
D. mysqli_fetch_assoc()

Answer: B. mysqli_query()

What does the mysqli_fetch_assoc() function do in PHP?


A. Fetches a single row as an associative array
B. Fetches a single row as a numeric array
C. Executes a MySQL query
D. Returns the number of affected rows

Answer: A. Fetches a single row as an associative array

Which of the following is used to close a MySQL connection in PHP?


A. mysql_close()
B. mysqli_close()
C. mysqli_disconnect()
D. mysql_disconnect()

Answer: B. mysqli_close()

Which PHP function is used to select a specific MySQL database to use?


A. mysqli_select_db()
B. mysqli_use_db()
C. mysqli_query()
D. mysqli_set_db()

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')");

Answer: A. mysqli_query($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

Answer: D. All of the above

What is the first step in executing a MySQL query using PHP?


A. Execute the query with mysqli_query()
B. Create a database table
C. Establish a connection with the MySQL server
D. Close the database connection

Answer: C. Establish a connection with the MySQL server

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.

The getdate() function returns

(a) An integer
(b) A floating-point number
(c) An array
(d) A string

The correct answer is: (c) An array

Explanation:

In PHP, the getdate() function returns an associative array containing information about the
current date and time.

Here’s an example of what getdate() might return:

What does the require() statement do in PHP?

(a) Includes and evaluates the specified file at runtime


(b) Includes a file only if a condition is true
(c) Includes a file, and continues execution even if the file is missing
(d) Includes a file, and halts execution if the file is missing

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.

What does the join() function do in PHP?

(a) Joins two strings


(b) Combines array elements into a string
(c) Splits a string into an array
(d) Merges two arrays

Correct Answer: (b) Combines array elements into a string


Explanation:
join() is an alias of implode(). It takes an array and returns a string containing the array
elements joined by a separator.

Which of the following is the correct syntax for using join()?

(a) join(array, separator)


(b) join(separator, array)
(c) join->(separator, array)
(d) join[array, separator]

Correct Answer: (b) join(separator, array)

What are superglobals in PHP?

(a) User-defined global variables


(b) Built-in variables that are always accessible
(c) Global functions for server interaction
(d) Arrays that store CSS classes

Correct Answer: (b) Built-in variables that are always accessible

Explanation:
Superglobals are built-in PHP arrays that are always accessible, regardless of scope.

Which of the following is NOT a PHP superglobal?

(a) $_POST
(b) $_GET
(c) $GLOBALS
(d) $SUPER

Correct Answer: (d) $SUPER

Explanation:
$SUPER does not exist. Common superglobals include $_GET, $_POST, $_SERVER, $_FILES,
$_COOKIE, $_SESSION, $_REQUEST, and $GLOBALS.

What does the $_SERVER superglobal contain?

(a) Environment variables only


(b) Client-side JavaScript
(c) Information about headers, paths, and script locations
(d) Uploaded file contents
Correct Answer: (c) Information about headers, paths, and script locations

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: (b) $_POST

What is the purpose of the $GLOBALS superglobal in PHP?

(a) To access server configuration


(b) To store cookies
(c) To access global variables from anywhere in the script
(d) To validate form input

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.

Which PHP superglobal is used to handle file uploads?

(a) $_FILES
(b) $_UPLOADS
(c) $_POST
(d) $_SERVER

Correct Answer: (a) $_FILES

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_star() starting a PHP session

$_SESSION[variable_name]=value_store

Introduction to cookies and Session handling:


A cookies is often used to identify a user . a cookies is a small file that the server embeds on the user’s
computer . each time the same computer request a page with a browser it will send the cookie too. With PHP
you can both create and retrieve cookies value.

Creating Cookies with PHP:-

A cookie is created with setCookies

Q. Which of the following statements about PHP sessions is true?

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.

Where are PHP session variables stored by default?

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.

. Which superglobal is used to access session variables?

A. $_POST
B. $_SESSION
C. $_COOKIE
D. $_GLOBALS

Correct Answer: B
Explanation: $_SESSION is the superglobal array used to access session variables.

Which function is used to remove all 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.

What does session_destroy() do?

A. Deletes all session files from the server


B. Unsets a specific session variable
C. Destroys all data registered to a session
D. Stops PHP execution

Correct Answer: C
Explanation: session_destroy() deletes all session data, but does not unset the $_SESSION
array unless manually done.

What is a cookie in PHP?

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.

Which PHP function is used to set a cookie?

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.

What is the correct syntax to set a cookie in PHP?

A. setcookie("user", "John", time()+3600);


B. cookie_set("user", "John");
C. create_cookie("John");
D. cookie("user", "John");
Correct Answer: A
Explanation: The correct syntax is setcookie(name, value, expire). This sets the cookie to
expire in 1 hour.

Which superglobal array is used to access cookie values in PHP?

A. $_SESSION
B. $_COOKIE
C. $_GET
D. $_SERVER

Correct Answer: B
Explanation: Cookie values can be retrieved using the $_COOKIE superglobal array.

When must the setcookie() function be called?

A. After HTML output


B. Inside <body>
C. Before any HTML output
D. At the end of the script

Correct Answer: C
Explanation: setcookie() must be called before any output is sent to the browser, like
header().

What is the default path of a cookie if not specified?

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

ii) while loop

iii) do-while loop

iv) foreach loop

a) Only iv)
b) i) and ii)
c) i), ii) and iii)
d) i), ii), iii) and iv)
View Answer
Answer: d

Which one of the following is the default PHP session name?


a) PHPSESSIONID
b) PHPIDSESS
c) PHPSESSID
d) PHPSESID
View Answer
Answer: c
Explanation: PHPSESSID is the default PHP session name. You can change this name by using the
session.name directive.

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

What will be the output of the following PHP code?

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.

. How many error levels are available in PHP?


a) 14
b) 15
c) 16
d) 17
View Answer
Answer: c
Explanation: Whenever the PHP engine encounters any problem that prevents a script from running properly it
generates an error message. There are sixteen error levels and each level is represented by an integer value
and an associated constant.

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

Advantage of PHP Function:

Code reusability

Less code

Easy to understand

User define function

<body>

<?php

function sayhello()

echo “hello php”;

sayHello();

?>

Function with argument:

function mytable($text)

print (“<table border=1><tr>”);

print(“<td> $text</td>”);
print(“</tr></table>”);

Processing a web form:

Make HTML and PHP file

Make HTML form with all input type

Get HTML form data in PHP

1: sum.php

<html>

<head>

<title> sum of number</title>

</head>

<form name =”client” method =”post” action=”sum 1.php”>

<input type =”text” name=”fname”>

<input type=”text” name=”lname”>

<input type=”submit” name =btnsumbit” value=”submit”>

</form>

</body>

</html>

2. sum1.php
<?php

$a=$_POST[“fname”];

$b=$_POST[“lname”];

$c=$a+$b;

echo “value of a is $c”;

?>

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.

Which is the correct syntax of defining a default argument value in PHP?

A. function function_name(type $argument_name : value) { /* function body*/ }


B. function function_name(type $argument_name , value) { /* function body*/ }
C. function function_name(type $argument_name = value) { /* function body*/ }
D. All of the above

Answer: C) function function_name(type $argument_name = value) { /* function body*/ }

Explanation:

The syntax of defining a default argument value in PHP is:

function function_name(type $argument_name = value) {


/* function body*/
}

Which is the correct syntax of defining function return type in PHP?

A. function function_name(type $arguments) : return_type { /* function body*/ }


B. function function_name(type $arguments) , return_type { /* function body*/ }
C. return_type function function_name(type $arguments) { /* function body*/ }
D. function return_type function_name(type $arguments) { /* function body*/ }

Answer: A) function function_name(type $arguments) : return_type { /* function body*/ }

Explanation:

The syntax of defining function return type in PHP is:

function function_name(type $arguments) : return_type {


/* function body*/
}

. 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.

Which is the correct example of an Indexed array in PHP?

A. $cities = array("Delhi"; "Mumbai"; "Banglore");


B. $cities = array("Delhi", "Mumbai", "Banglore");
C. $cities = new array("Delhi", "Mumbai", "Banglore");
D. $cities = new array(3) ("Delhi", "Mumbai", "Banglore");

Answer: B) $cities = array("Delhi", "Mumbai", "Banglore");

Explanation:

The valid example of an Indexed array in PHP is:

$cities = array("Delhi", "Mumbai", "Banglore");

Which is the correct example of an Associative array in PHP?

A. $person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");


B. $person = array("Alvin"=>"Delhi"; "Alex"=>"Mumbai"; "Bhavik"=>"Banglore");
C. $person = new array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");
D. $person = new array("Alvin"=>"Delhi"; "Alex"=>"Mumbai"; "Bhavik"=>"Banglore");

Answer: A) $person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");

Explanation:

The valid example of an Associative array in PHP is:

$person = array("Alvin"=>"Delhi", "Alex"=>"Mumbai", "Bhavik"=>"Banglore");

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

Answer: D) All of the above

Explanation:

All of the above PHP functions are used to compare arrays and returns the differences.

What is the difference between array_diff() and array_diff_assoc() functions?

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:

The difference between array_diff() and array_diff_assoc() functions is:


The array_diff() compares the values only while array_diff_assoc() compares the keys and
values.

Which PHP function is used to sort multi-dimensional arrays?

A. array_sort()
B. sort()
C. multisort()
D. array_multisort()

Answer: D) array_multisort()

Explanation:

The PHP function array_multisort() is used to sort multi-dimensional arrays.

What is the use of PHP sort() function?

A. Sorts an indexed array


B. Sorts an associative array
C. Sorts a multi-dimensional array
D. Sorts any kind of array
Answer: A) Sorts an indexed array

Explanation:

The PHP function sort() is used to sort an indexed array.

Which PHP function is used to sort an indexed array in descending order?

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.

Which PHP function is used to return the current element in an array?

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.

Which PHP function is used to check if a specified value exists 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.

What is the use of PHP $_SERVER variable?

A. To update the content on the server


B. To access the information about headers, paths, and script locations
C. To access and update the database records on the server
D. All of the above

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

We can create string

Single quoted

<?php
$str=’hello’;

Echo $str;

?>

Using Double quoted

<?php

$str=”hello”;

Echo $str;

?>

Str_word_count() function: count the words in string

Strrev() function : reverse the string

Strtoupper() function convert string into upper case

Strtolower() function : convert string into lower case

54) Which function is used to find the length of a string in PHP?

A. strlen()
B. count()
C. strcount()
D. length()

Answer: A. strlen()

55) Which function is used to find the position of a substring in a string?

A. strpos()
B. substr()
C. str_find()
D. str_search()

Answer: A. strpos()

56) What is the output of substr("abcdef", 1, 3)?

A. bcd
B. abc
C. cde
D. def

Answer: A. bcd

57) Which function splits a string into an array?

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()

60) Which function returns part of a string starting at a given position?

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)

62) How do you concatenate two strings in PHP?


a) Using the + operator

b) Using the . operator

c) Using the & operator

d) Using the && operator

ans b)

63) Which of the following is the correct way to declare a string in PHP?
a) $string = 'Hello World';

b) $string = "Hello World";

c) Both a) and b)

d) None of the above

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');

b) str_replace('world', 'PHP', 'Hello world');

c) substr_replace('Hello world', 'PHP', 5);

d) None of the above

Answer:

b) str_replace('world', 'PHP', 'Hello world');


Explanation:

The str_replace() function is used for string replacement in PHP.

66) Which of the following is not a valid way to declare a multiline string in PHP?
a) Using single quotes

b) Using double quotes

c) Using heredoc syntax

d) Using nowdoc syntax

Answer:

a) Using single quotes

Explanation:

Multiline strings can be declared using double quotes, heredoc, or nowdoc syntax, but not with
single quotes.

Control statement in pHP


Conditional statement: there are two type of conditional statement in php

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++;

Php do while loop:

It guaranteed to run at least once


Do

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;

}
}

Php continue statement:

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”;

46) What will be the output of the following PHP code?

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

ans a) because x is set to zero by default

47) What will be the output of the following PHP code?

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

ans c) error because echo in if statement does not return anything

48) What will be the output of the following PHP code?

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

ans a ) as break is not put inside the case statement

49) What will be the output of the following PHP code?

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

50) What will be the output of the following PHP code?

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.

51) What will be the output of the following PHP code?

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.

52) What will be the output of the following PHP code?

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.

53) . What does PDO stand for?


a) PHP Database Orientation
b) PHP Data Orientation
c) PHP Data Object
d) PHP Database Object
View Answer
Answer: c
Explanation: PDO stands for PHP Data Object. The PDO class provides a common interface to different
database applications.

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

a) Drek kolkevi b) rasmus lerdorf c) Sholes d) Rumal ans b)

3)What does PHP stand for

a) Preprocessor home page b) pretext hypertext processor c) hypertext preprocessor


c) Personal hyper processor ans c)

4)choose the correct syntax of PHP

a) <PHP> b) <?php > c) ?php ? d) <?php ?> ans d)

5) Which of the following is the correct way to add a comment in PHP codes

a) # b) // c) /* */ d) all of the mentioned ans d

6) which is not a valid variable scope in PHP

a)local b) global c) static d) external ans d)

7) how many variable scope are there in php

a)1 b) 2 c) 3 d) d ans c local global and static

8) what is the name of an array that stores all global variable in PHP

a) $GLOBAL[] b) $global[] c) $GLOBALS[] D) $PHP_GLOBALS[] ANS C)

9) which php constant return the largest integer supported

a) INT_MAX b) MAX_INT c) INTSIZE d) PHP_INT_SIZE ANS D

10) which PHP constant returns the size of an integer in bytes

a)INT_SIZE B) SIZE_INT C) INTSIZE D) PHP_INT_SIZE ANS(D)

11)which of the following start with _ _(double underscore ) in php

a) input constant b) user defined constant c) Magic constant d) default constant ans c)

12) important fact :

Strpos() function is used to search for character /text in a string

13) Important Fact:

PEAR(PHP Extension and repository) PEAR is a framework and repository

14) what is Isset() it is used to check whether a variable is set or not


15) Which of the following is the correct way to define a variable in PHP?

A) var $name = "John";


B) $name = "John";
C) variable name = "John";
D) define $name = "John";

Answer: B) $name = "John";

16) In PHP, what does the define() function do?

A) It creates a new variable.


B) It creates a new constant.
C) It changes the value of a variable.
D) It defines a function.

Answer: B) It creates a new constant.

17) How do you declare a constant in PHP?

A) constant name = "value";


B) define("name", "value");
C) $name = "value";
D) const "name" = "value";

Answer: B) define("name", "value");

18) Which of the following is NOT a valid constant name in PHP?

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

Answer: A) $int (PHP uses int, not $int)


20) In PHP, which of the following variables are case-sensitive?

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

Answer: D) Both A and C

22) Which of the following is true about PHP variables?

A) Variables are global by default.


B) Variables must be declared before use.
C) Variables in PHP are case-insensitive.
D) Variables must always begin with a dollar sign ($).

Answer: D) Variables must always begin with a dollar sign ($).

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) It will modify the value of the constant.


B) It will cause a fatal error.
C) The value of the constant will be overwritten.
D) It will change the constant’s value to null.

Answer: B) It will cause a fatal error.


25) missing of semicolon no dollar sign are example of

a) rendering problem

b) installation related 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

a) sequence of key value

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.

28) finding nonempty elements in the array we use

a) is_array

b) sizeof()

c)array_count()

d) count()
Ans d)

29) To retrive a value from an array we can use its

a) name

b) location

c) index

d) none

ans c)

30) which of the following is correct about constant vs variable in php

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.

31) the php syntax is most similar to

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:

• use of curly braces {} for code blocks


• semicolons ; to end statements
• use of $ to denote variables (like in Perl)
• control structures (like if, while, for) follow the C-style syntax

32) What will be the output of the following PHP code?

<?php
echo strpos("Hello, Includehelp!", "Includehelp!");
?>

A. 6
B. 7
C. 8
D. -1

Answer: B) 7

33) Which function is used to replace text within a string?

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?

A. strupper() and strlower()


B. str_toupper() and str_tolower()
C. toupper() and tolower()
D. strtoupper() and strtolower()

Answer: D) strtoupper() and strtolower()

Explanation:

The strtoupper() function is used to convert the string to uppercase. And, strtolower() function
is used to convert the string to lowercase.

37) PHP NaN stands for ____.

A. not-a-number
B. not-a-numerical
C. nothing-a-number
D. numeric-a-number

Answer: A) not-a-number

38) Which PHP function is used to get the random number?

A. random()
B. randomize()
C. rand()
D. randnumbers()

Answer: C) rand()

39) What is the scope of a constant in PHP?

A. Local
B. Global
C. Static
D. Fixed

Answer: B) Global

Array are define following type

Index array:

$season=array(“summer”, “winter”, “sping”, “autumn”);


Echo “season are $season[1]”;

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

42) How do you create an associative array?


A) $arr = array("a", "b", "c");
B) $arr = array("name" => "John", "age" => 30);
C) $arr = ("name" = "John", "age" = 30);
D) $arr = {"name": "John", "age": 30};
Correct Answer: B

Multidimensional array: multidimensional array is also called as array of array

<?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/>”;

?>

43) What is a multidimensional array in PHP?


A) An array with more than 10 elements
B) An array that contains other arrays as elements
C) An array with both numeric and string keys
D) An array that uses classes
Correct Answer: B

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

45) Which function is used to loop through a multidimensional array?


A) array_loop()
B) foreach()
C) for_array()
D) loop_array()
Correct Answer: B

For each loop

foreach($array as $var)

You might also like