PHP UNIT 1
1. What does PHP stand for?
A) Personal Home Page
B) Private Home Page
C) PHP: Hypertext Preprocessor
D) Preprocessed Hypertext Processor
Answer: C) PHP: Hypertext Preprocessor
2. Which of the following is the correct way to start a PHP block of code?
A) <php>
B) <script>
C) <?php ... ?>
D) <?php> ... </php>
Answer: C) <?php ... ?>
3. PHP files have a default file extension of:
A) .html
B) .php
C) .xml
D) .js
Answer: B) .php
4. Which of the following is used to output text in PHP?
A) echo
B) print
C) write
D) Both A and B
Answer: D) Both A and B
5. PHP is a __________ scripting language.
A) Client-side
B) Server-side
C) Database
D) Compiler-based
Answer: B) Server-side
6. Variables in PHP start with which symbol?
A) !
B) #
C) $
D) @
Answer: C) $
7. Which of the following is NOT a PHP data type?
A) String
B) Integer
C) Float
D) Character
Answer: D) Character
8. How do you write a single-line comment in PHP?
A) <!-- comment -->
B) // comment
C) # comment
D) Both B and C
Answer: D) Both B and C
9. Who is the original creator of PHP?
A) Rasmus Lerdorf
B) James Gosling
C) Brendan Eich
D) Dennis Ritchie
Answer: A) Rasmus Lerdorf
10. In which year was PHP originally created?
A) 1991
B) 1994
C) 1995
D) 1998
Answer: C) 1995
11. What was the original name of PHP?
A) Personal Home Pages
B) PHP Scripts
C) PHP Markup
D) PHP CodeBase
Answer: A) Personal Home Pages
12. PHP was originally developed to:
A) Create desktop applications
B) Manage databases
C) Track visits to Rasmus Lerdorf's online resume
D) Replace JavaScript
Answer: C) Track visits to Rasmus Lerdorf's online resume
13. Which version of PHP introduced object-oriented programming support?
A) PHP 3
B) PHP 4
C) PHP 5
D) PHP 6
Answer: C) PHP 5
14. The Zend Engine was introduced in which version of PHP?
A) PHP 2
B) PHP 3
C) PHP 4
D) PHP 5
Answer: C) PHP 4
15. Who developed the Zend Engine for PHP?
A) Zeev Suraski and Andi Gutmans
B) Rasmus Lerdorf
C) Linus Torvalds
D) Guido van Rossum
Answer: A) Zeev Suraski and Andi Gutmans
16. What major feature was introduced in PHP 7?
A) Better support for MySQL
B) Built-in server
C) Just-In-Time (JIT) compilation
D) Performance improvements and scalar type declarations
Answer: D) Performance improvements and scalar type declarations
17. Which version of PHP introduced the Zend Engine?
A) PHP 3
B) PHP 4
C) PHP 5
D) PHP 7
Answer: B) PHP 4
18. Which PHP version introduced full support for Object-Oriented
Programming (OOP)?
A) PHP 3
B) PHP 4
C) PHP 5
D) PHP 6
Answer: C) PHP 5
19. Which of the following versions of PHP was never officially released?
A) PHP 5.6
B) PHP 6
C) PHP 7
D) PHP 8
Answer: B) PHP 6
20. PHP 7 introduced which major feature?
A) Multithreading
B) Deprecated short open tags
C) Scalar type declarations
D) Built-in AI support
Answer: C) Scalar type declarations
21. What is the key performance improvement in PHP 7 compared to PHP 5?
A) Multi-core support
B) JIT compilation
C) Memory optimization and faster execution
D) GPU acceleration
Answer: C) Memory optimization and faster execution
22. In which PHP version was the Just-In-Time (JIT) compiler introduced?
A) PHP 5.5
B) PHP 7.4
C) PHP 8.0
D) PHP 8.2
Answer: C) PHP 8.0
23. Which PHP version dropped support for Windows XP and Windows
Server 2003?
A) PHP 5.6
B) PHP 7.0
C) PHP 7.2
D) PHP 8.0
Answer: B) PHP 7.0
24. Which version of PHP introduced union types and attributes
(annotations)?
A) PHP 7.2
B) PHP 7.4
C) PHP 8.0
D) PHP 8.1
Answer: C) PHP 8.0
25. Which of the following is a key feature of PHP?
A) PHP is a compiled language
B) PHP runs only on Windows
C) PHP is an open-source server-side scripting language
D) PHP does not support databases
Answer: C) PHP is an open-source server-side scripting language
26. PHP scripts are executed on:
A) Client browser
B) PHP interpreter on client-side
C) Server
D) Database
Answer: C) Server
27. Which of the following databases can PHP work with?
A) MySQL
B) PostgreSQL
C) SQLite
D) All of the above
Answer: D) All of the above
28. PHP can be embedded into which type of files?
A) XML
B) JavaScript
C) HTML
D) Python
Answer: C) HTML
29. Which of the following is NOT a feature of PHP?
A) Platform independent
B) Strongly typed language
C) Easy to learn
D) Supports OOP
Answer: B) Strongly typed language
30. PHP supports which programming paradigm(s)?
A) Procedural
B) Object-Oriented
C) Both A and B
D) None
Answer: C) Both A and B
31. PHP supports error handling using:
A) try-catch blocks
B) error_reporting() function
C) set_error_handler() function
D) All of the above
Answer: D) All of the above
32. Which feature allows PHP to handle forms and user input easily?
A) PHP tags
B) Superglobals like $_POST, $_GET
C) Cookies
D) Sessions
Answer: B) Superglobals like $_POST, $_GET
33. Which of the following is a major advantage of PHP?
A) It is a paid software
B) It runs only on Windows
C) It is open-source and free
D) It requires a Java runtime
Answer: C) It is open-source and free
34. PHP can run on which operating systems?
A) Windows
B) Linux
C) macOS
D) All of the above
Answer: D) All of the above
35. What makes PHP suitable for developing dynamic websites?
A) It is compiled before execution
B) It integrates easily with HTML and databases
C) It uses XML by default
D) It is a desktop application tool
Answer: B) It integrates easily with HTML and databases
36. Which of the following databases is PHP commonly used with?
A) MongoDB
B) PostgreSQL
C) MySQL
D) All of the above
Answer: D) All of the above
37. Why is PHP considered easy to learn?
A) It uses XML-based syntax
B) It has a simple syntax similar to C and Java
C) It only supports object-oriented programming
D) It is a low-level programming language
Answer: B) It has a simple syntax similar to C and Java
38. What is an advantage of PHP’s large community?
A) Expensive paid support
B) Access to free tutorials, libraries, and tools
C) Less documentation available
D) Limited third-party support
Answer: B) Access to free tutorials, libraries, and tools
39. PHP supports which of the following programming paradigms?
A) Procedural
B) Object-Oriented
C) Both A and B
D) None
Answer: C) Both A and B
40. Which of the following is a performance-related advantage of PHP
(especially in newer versions)?
A) Slower execution
B) High memory usage
C) JIT compilation and improved speed
D) Requires manual memory management
Answer: C) JIT compilation and improved speed
41. Which of the following is required to execute PHP code?
A) Web browser only
B) PHP interpreter or server with PHP support
C) Java SDK
D) Node.js
Answer: B) PHP interpreter or server with PHP support
42. Which server is commonly used to run PHP scripts?
A) IIS
B) Apache
C) Nginx
D) All of the above
Answer: D) All of the above
43. Which of the following is a popular package that includes PHP, MySQL,
and Apache for local development?
A) WAMP
B) LAMP
C) XAMPP
D) All of the above
Answer: D) All of the above
44. What minimum version of PHP is recommended for modern applications
(as of 2025)?
A) PHP 5.6
B) PHP 7.0
C) PHP 7.4
D) PHP 8.0 or higher
Answer: D) PHP 8.0 or higher
45. Which operating systems can run a PHP development environment?
A) Windows
B) Linux
C) macOS
D) All of the above
Answer: D) All of the above
46. Which component is typically used with PHP for database management?
A) Apache
B) MySQL
C) HTML
D) CSS
Answer: B) MySQL
47. What is the role of the PHP interpreter?
A) Converts PHP code to machine code
B) Translates PHP code into HTML to be sent to the client
C) Compresses PHP files
D) Encrypts PHP scripts
Answer: B) Translates PHP code into HTML to be sent to the client
48. To run PHP scripts locally, which of the following is most essential?
A) Only a text editor
B) Database connection
C) Web server + PHP interpreter
D) Java virtual machine
Answer: C) Web server + PHP interpreter
49. Which of the following is a commonly used package for installing PHP,
Apache, and MySQL on Windows?
A) LAMP
B) Xcode
C) XAMPP
D) MAMP
Answer: C) XAMPP
50. What is the default folder for placing .php files in XAMPP on Windows?
A) /php
B) htdocs
C) www
D) root
Answer: B) htdocs
51. On Linux (Ubuntu), which command installs PHP with Apache?
A) sudo install php
B) apt-get phpinstall
C) sudo apt install php libapache2-mod-php
D) yum php
Answer: C) sudo apt install php libapache2-mod-php
52. Which configuration file is used to customize PHP behavior?
A) httpd.conf
B) my.ini
C) php.conf
D) php.ini
Answer: D) php.ini
53. To apply changes made in php.ini, you should:
A) Refresh the browser
B) Restart your PC
C) Restart the web server
D) Reinstall PHP
Answer: C) Restart the web server
54. What tool can be used to verify that PHP is correctly installed?
A) Running a .js file
B) Creating a phpinfo() test page
C) Running a mysql command
D) Opening index.html
Answer: B) Creating a phpinfo() test page
55. On macOS, which package manager can be used to install PHP?
A) Brew
B) Pip
C) NPM
D) Composer
Answer: A) Brew
56. To add PHP to the system PATH in Windows, you should:
A) Add Apache to the PATH
B) Add the folder containing php.exe to environment variables
C) Add MySQL to PATH
D) Install a PHP IDE
Answer: B) Add the folder containing php.exe to environment variables
57. What is Apache?
A) A text editor
B) A programming language
C) A web server software
D) A database system
Answer: C) A web server software
58. Which software package includes Apache, PHP, and MySQL for
Windows?
A) Node.js
B) XAMPP
C) Visual Studio
D) Notepad++
Answer: B) XAMPP
59. After installing XAMPP, how do you start the Apache server?
A) Use Task Manager
B) From the Windows Services panel
C) From the XAMPP Control Panel
D) Run apache.exe manually
Answer: C) From the XAMPP Control Panel
60. What is the default port Apache uses on Windows?
A) 21
B) 80
C) 3306
D) 443
Answer: B) 80
61. If port 80 is already in use, what can you do?
A) Disable Windows Firewall
B) Restart your computer
C) Change Apache’s port in the httpd.conf file
D) Uninstall Apache
Answer: C) Change Apache’s port in the httpd.conf file
62. What file must be edited to configure Apache settings manually?
A) apache_settings.ini
B) config.xml
C) httpd.conf
D) php.ini
Answer: C) httpd.conf
63. In Apache configuration, which directive sets the folder where your
website files are stored?
A) DocumentRoot
B) DirectoryIndex
C) ServerRoot
D) ScriptAlias
Answer: A) DocumentRoot
64. To test if Apache is running properly, you should:
A) Check Task Manager
B) Open localhost in a web browser
C) Ping 127.0.0.1
D) Use the command line only
Answer: B) Open localhost in a web browser
65. How can PHP code be embedded in an HTML file?
A) Using <script> tags
B) Using <php> tags
C) Using <?php ... ?> tags
D) Using <embed> tags
Answer: C) Using <?php ... ?> tags
66. What is the correct syntax to output “Hello World” in a PHP-embedded
HTML file?
A) echo "Hello World"
B) print("Hello World")
C) <?php echo "Hello World"; ?>
D) Hello World
Answer: C) <?php echo "Hello World"; ?>
67. Where is PHP code executed when embedded in HTML?
A) Client-side browser
B) JavaScript engine
C) Web server
D) HTML interpreter
Answer: C) Web server
68. Which of the following is a valid way to mix PHP and HTML in one file?
A)
html
CopyEdit
<html>
<body>
<?php echo "Welcome!"; ?>
</body>
</html>
B)
php
CopyEdit
<html>
<body>
echo "Welcome!";
</body>
</html>
C)
html
CopyEdit
<html>
<body>
<?echo "Welcome!";?>
</body>
</html>
D) Both A and C
Answer: D) Both A and C
69. Which file extension allows PHP to be parsed when embedded in HTML?
A) .html
B) .php
C) .js
D) .xml
Answer: B) .php
70. What will happen if you embed PHP code in an .html file without server
configuration?
A) The PHP code runs normally
B) The browser crashes
C) The server ignores the PHP code and shows it as text
D) It automatically converts to .php
Answer: C) The server ignores the PHP code and shows it as text
71. What function is commonly used to output text in PHP embedded in
HTML?
A) write()
B) alert()
C) echo
D) display()
Answer: C) echo
72. Which of the following is NOT a valid data type in PHP?
A) Integer
B) Boolean
C) Character
D) Array
Answer: C) Character
73. What is the result of the following PHP code?
php
CopyEdit
$x = "5" + 10;
echo $x;
A) 510
B) 15
C) "5"
D) Error
Answer: B) 15
(PHP automatically converts the string "5" to an integer.)
74. Which PHP data type is used to represent a collection of key-value pairs?
A) Object
B) Array
C) String
D) Float
Answer: B) Array
75. What data type does the var_dump() function show for the value true?
A) string(4) "true"
B) boolean true
C) int(1)
D) string(1) "1"
Answer: B) boolean true
76. What will gettype(3.14) return in PHP?
A) integer
B) float
C) double
D) number
Answer: B) float
77. Which function is used to determine the data type of a variable?
A) type()
B) typeof()
C) gettype()
D) var_type()
Answer: C) gettype()
78. What type is returned by the expression is_array([]) in PHP?
A) true
B) 1
C) boolean
D) bool(true)
Answer: D) bool(true)
79. Which of the following is a scalar data type in PHP?
A) Array
B) Object
C) String
D) Resource
Answer: C) String
80. Which of the following is used for a single-line comment in PHP?
A) <!-- Comment -->
B) // Comment
C) /* Comment */
D) ## Comment
Answer: B) // Comment
81. Which of the following is used for multi-line comments in PHP?
A) <!-- Comment -->
B) # Comment
C) /* Comment */
D) // Comment //
Answer: C) /* Comment */
82. What will the following code output?
php
CopyEdit
<?php
// echo "Hello";
/* echo "Hi!"; */
echo "Hey!";
?>
A) Hello
B) Hi!
C) Hey!
D) Nothing
Answer: C) Hey!
83. Which of the following symbols can be used to create a comment in PHP?
A) #
B) //
C) /* ... */
D) All of the above
Answer: D) All of the above
84. Which of these comments will cause an error in PHP?
A) // This is a comment
B) # This is a comment
C) /* This is a comment */
D) <!– This is a comment –>
Answer: D) <!– This is a comment –>
(That’s an HTML comment, not valid in PHP.)
85. What is the purpose of using comments in PHP code?
A) To increase execution speed
B) To hide errors
C) To document code for developers
D) To define variables
Answer: C) To document code for developers
86. Which symbol is used to declare a variable in PHP?
A) @
B) &
C) $
D) %
Answer: C) $
87. What is the correct way to declare a constant in PHP?
A) const MY_CONSTANT = "value";
B) define("MY_CONSTANT", "value");
C) constant("MY_CONSTANT", "value");
D) Both A and B
Answer: D) Both A and B
88. Which of the following is a valid variable name in PHP?
A) $1variable
B) $_variable
C) $variable_name_@
D) @variable
Answer: B) $_variable
89. What is the scope of a variable declared inside a function in PHP?
A) Global
B) Local
C) Static
D) Both A and B
Answer: B) Local
90. Can you change the value of a constant in PHP after it is defined?
A) Yes
B) No
C) It depends on the data type
D) Only if it is a global constant
Answer: B) No
91. What will happen if you try to access an undefined variable in PHP?
A) The script will terminate with an error
B) PHP will treat it as NULL
C) The script will automatically create the variable
D) PHP will throw a warning but continue execution
Answer: D) PHP will throw a warning but continue execution
92. Which of the following is a valid way to define a constant in PHP?
A) define("PI", 3.14);
B) constant("PI", 3.14);
C) const PI = 3.14;
D) All of the above
Answer: D) All of the above
93. What is the correct way to define a variable that is global in PHP?
A) global $varName;
B) public $varName;
C) global($varName);
D) extern $varName;
Answer: A) global $varName;
94. Which of the following can be used to check if a variable is set in PHP?
A) isset($var)
B) is_set($var)
C) check_set($var)
D) defined($var)
Answer: A) isset($var)
95. Which of the following will not be allowed in PHP variable names?
A) $variable_name
B) $var123
C) $1variable
D) $_variable
Answer: C) $1variable
96. Which of the following is the correct way to declare an indexed array in
PHP?
A) $arr = (1, 2, 3);
B) $arr = [1, 2, 3];
C) $arr = array(1, 2, 3);
D) Both B and C
Answer: D) Both B and C
97. What is the default index for an indexed array in PHP if no index is
provided?
A) 0
B) 1
C) -1
D) Null
Answer: A) 0
98. Which of the following functions is used to get the length of an array in
PHP?
A) length($array)
B) size($array)
C) count($array)
D) array_size($array)
Answer: C) count($array)
99. Which of the following PHP functions is used to merge two or more
arrays?
A) merge()
B) array_merge()
C) combine()
D) array_combine()
Answer: B) array_merge()
100. How would you access the value 3 in the following PHP array?
php
CopyEdit
$array = [1, 2, 3, 4];
A) $array[2]
B) $array(3)
C) $array[3]
D) $array[1]
Answer: A) $array[2]
101. Which of the following is used to create an associative array in PHP?
A) $arr = array(0 => "apple", 1 => "banana");
B) $arr = ["a" => "apple", "b" => "banana"];
C) $arr = (apple, banana);
D) $arr = array("apple", "banana");
Answer: B) $arr = ["a" => "apple", "b" => "banana"];
102. What will the following code output?
php
CopyEdit
$fruits = array("apple", "banana", "cherry");
echo $fruits[1];
A) apple
B) banana
C) cherry
D) 1
Answer: B) banana
103. What is the result of the following PHP code?
php
CopyEdit
$array = ["name" => "John", "age" => 25];
echo $array["name"];
A) John
B) 25
C) name
D) age
Answer: A) John
104. Which of the following functions is used to check if a value exists in an
array in PHP?
A) is_array()
B) array_key_exists()
C) in_array()
D) array_exists()
Answer: C) in_array()
105. How can you remove the last element from an array in PHP?
A) array_pop()
B) array_shift()
C) array_remove()
D) array_delete()
Answer: A) array_pop()
106. How do you create an indexed array in PHP?
A) $arr = array(1, 2, 3);
B) $arr = (1, 2, 3);
C) $arr = [1 => "a", 2 => "b", 3 => "c"];
D) $arr = "1, 2, 3";
Answer: A) $arr = array(1, 2, 3);
107. How do you access the second element of the following array?
php
CopyEdit
$array = [10, 20, 30];
A) $array[0]
B) $array[1]
C) $array[2]
D) $array[3]
Answer: B) $array[1]
108. Which of the following creates an associative array in PHP?
A) $arr = array("name" => "John", "age" => 30);
B) $arr = [1 => "apple", 2 => "banana"];
C) $arr = array(1, 2, 3);
D) $arr = ("John", 30);
Answer: A) $arr = array("name" => "John", "age" => 30);
109. How would you access the "age" value from the following associative
array?
php
CopyEdit
$person = array("name" => "John", "age" => 30);
A) $person["John"]
B) $person["name"]
C) $person["age"]
D) $person[30]
Answer: C) $person["age"]
110. How do you create a multidimensional array in PHP?
A) $arr = array([1, 2], [3, 4]);
B) $arr = array(1, 2, array(3, 4));
C) $arr = array(1, 2, 3);
D) $arr = [1, [2, 3], 4];
Answer: B) $arr = array(1, 2, array(3, 4));
111. How would you access the element 4 in the following multidimensional
array?
php
CopyEdit
$array = array(array(1, 2), array(3, 4));
A) $array[0][1]
B) $array[1][1]
C) $array[1][0]
D) $array[0][0]
Answer: B) $array[1][1]
112. How would you add an element "50" to the end of an array $arr in PHP?
A) array_push($arr, 50);
B) $arr[] = 50;
C) push($arr, 50);
D) Both A and B
Answer: D) Both A and B
113. How do you remove the first element of an array in PHP?
A) array_shift($arr);
B) array_pop($arr);
C) array_remove($arr, 0);
D) shift_array($arr);
Answer: A) array_shift($arr);
114. What will be the output of the following code?
php
CopyEdit
$arr = array(1, 2, 3, 4);
echo $arr[2];
A) 1
B) 2
C) 3
D) 4
Answer: C) 3
115. Which function is used to check if an index exists in an array?
A) isset()
B) array_index_exists()
C) array_key_exists()
D) key_exists()
Answer: C) array_key_exists()
116. Which function is used to find the length of a string in PHP?
A) strlen()
B) strlength()
C) count()
D) string_length()
Answer: A) strlen()
117. What is the correct way to concatenate two strings in PHP?
A) concat("Hello", " World");
B) "Hello" + " World";
C) "Hello" . " World";
D) "Hello" . " + " World";
Answer: C) "Hello" . " World";
118. How would you convert a string to uppercase in PHP?
A) strtoupper("hello");
B) string_to_upper("hello");
C) upper("hello");
D) strUpper("hello");
Answer: A) strtoupper("hello");
119. Which of the following functions is used to replace a substring within a
string in PHP?
A) replace()
B) substr_replace()
C) str_replace()
D) replace_substr()
Answer: C) str_replace()
120. What will the following PHP code output?
php
CopyEdit
$txt = "Hello, world!";
echo substr($txt, 7, 5);
A) Hello
B) world
C) , wor
D) world!
Answer: B) world
121. How do you search for a specific character in a string in PHP?
A) find()
B) strpos()
C) indexOf()
D) search()
Answer: B) strpos()
122. Which function is used to strip whitespace from both ends of a string in
PHP?
A) trim()
B) strip_tags()
C) clean_string()
D) remove_whitespace()
Answer: A) trim()
123. Which of the following functions converts a string to lowercase in PHP?
A) lowercase()
B) to_lower()
C) strtolower()
D) string_lowercase()
Answer: C) strtolower()
124. What will the following code output?
php
CopyEdit
$txt = "Hello, world!";
echo strlen($txt);
A) 12
B) 13
C) 14
D) 11
Answer: B) 13
125. Which function is used to find the position of the first occurrence of a
substring in a string?
A) strpos()
B) indexOf()
C) substr()
D) find()
Answer: A) strpos()
126. Which of the following is the assignment operator in PHP?
A) =
B) ==
C) ===
D) =>
Answer: A) =
127. What will the following code output?
php
CopyEdit
$x = 10;
$y = 5;
echo $x + $y;
A) 15
B) 50
C) 5
D) Error
Answer: A) 15
128. Which operator is used for string concatenation in PHP?
A) +
B) &
C) .
D) ,
Answer: C) .
129. What is the result of 5 == "5" in PHP?
A) true
B) false
C) error
D) null
Answer: A) true
(PHP performs type coercion and compares values regardless of type.)
130. Which of the following is the correct comparison operator for equality in
PHP?
A) =
B) ==
C) ===
D) ><
Answer: B) ==
131. What does the !== operator do in PHP?
A) Checks if two values are equal
B) Checks if two values are not equal and not of the same type
C) Checks if two values are of the same type
D) Checks if two values are identical
Answer: B) Checks if two values are not equal and not of the same type
132. Which of the following is the correct way to increment a variable $x by 1?
A) $x++
B) $x = x + 1
C) ++$x
D) All of the above
Answer: D) All of the above
133. What is the result of 10 % 3 in PHP?
A) 1
B) 2
C) 3
D) 0
Answer: B) 2
(The modulo operator returns the remainder of the division.)
134. Which of the following operators is used for logical AND in PHP?
A) &&
B) ||
C) and
D) &
Answer: A) &&
135. What does the ++$x operator do in PHP?
A) Increments $x after using its value
B) Increments $x before using its value
C) Does not increment $x
D) Compares $x with 1
Answer: B) Increments $x before using its value
(This is the pre-increment operator.)
136. Which of the following operators has the highest precedence in PHP?
A) &&
B) =
C) ++
D) +
Answer: C) ++
(The increment and decrement operators have the highest precedence.)
137. What will be the result of the following code?
php
CopyEdit
$x = 5 + 3 * 2;
echo $x;
A) 16
B) 11
C) 13
D) 10
Answer: B) 11
(Multiplication has higher precedence than addition.)
138. Which of the following has the lowest precedence in PHP?
A) &&
B) ==
C) +
D) =
Answer: D) =
(Assignment operator (=) has the lowest precedence.)
139. What will be the output of the following PHP code?
php
CopyEdit
$a = 10;
$b = 5;
$c = 2;
$result = $a + $b * $c;
echo $result;
A) 20
B) 30
C) 12
D) 25
Answer: B) 30
(Multiplication is evaluated before addition, so 5 * 2 = 10 and then 10 + 10 = 30.)
140. Which operator has higher precedence in PHP?
A) ==
B) &&
C) +
D) =
Answer: B) &&
(Logical operators like && have higher precedence than comparison operators like ==.)
141. What will be the output of the following PHP code?
php
CopyEdit
$x = 5;
$y = 2;
$z = 3;
$result = $x * $y + $z;
echo $result;
A) 25
B) 16
C) 21
D) 13
Answer: B) 16
(Multiplication has higher precedence than addition, so 5 * 2 = 10, then 10 + 3 = 16.)
142. What is the correct order of precedence of the following operators in
PHP?
+, *, =, &&
A) +, *, &&, =
B) *, +, &&, =
C) &&, +, *, =
D) *, +, =, &&
Answer: B) *, +, &&, =
143. Which of the following is the correct order of precedence from highest to
lowest?
A) ++, *, &&, =
B) *, ++, &&, =
C) ++, &&, *, =
D) ++, *, =, &&
Answer: A) ++, *, &&, =
144. In the expression 3 + 4 * 2, what is the correct order of operations?
A) Add first, then multiply
B) Multiply first, then add
C) Both operations happen simultaneously
D) The expression will cause an error
Answer: B) Multiply first, then add
(Multiplication has higher precedence than addition.)
145. What is the correct order of precedence for the following operators?
++, &&, ==, +
A) ++, +, ==, &&
B) ++, +, &&, ==
C) ==, ++, &&, +
D) +, ==, &&, ++
Answer: A) ++, +, ==, &&
146. What is an expression in PHP?
A) A string of text
B) A combination of variables, constants, operators, and functions that produce a value
C) A statement that always returns true or false
D) A block of code inside curly braces {}
Answer: B) A combination of variables, constants, operators, and functions that produce a value
147. Which of the following is a valid PHP expression?
A) $x = 5 + 3;
B) echo "Hello World";
C) if ($x > 5) { echo "Greater"; }
D) for ($i = 0; $i < 10; $i++) { echo $i; }
Answer: A) $x = 5 + 3;
(This is an expression because it produces a value and assigns it to $x.)
148. Which of the following expressions will result in the value 15?
A) 5 + 5 * 2
B) (5 + 5) * 2
C) 5 + 5 + 2
D) 5 * 3
Answer: B) (5 + 5) * 2
(Parentheses are evaluated first, so 10 * 2 = 15.)
149. What will the following PHP expression output?
php
CopyEdit
$x = 5;
$y = 10;
$z = $x + $y;
echo $z;
A) 5
B) 10
C) 15
D) 50
Answer: C) 15
(The sum of $x and $y is 15, so $z is 15.)
150. Which of the following is not considered an expression in PHP?
A) 5 + 3
B) x++
C) if ($x > 5) { echo "Greater"; }
D) while ($x < 10) { $x++; }
Answer: C) if ($x > 5) { echo "Greater"; }
(This is a control structure, not an expression.)
151. What will the following code output?
php
CopyEdit
$x = 4;
$y = 2;
$z = $x * $y + 3;
echo $z;
A) 11
B) 14
C) 10
D) 7
Answer: A) 11
(The multiplication is performed first (4 * 2 = 8), then the addition (8 + 3 = 11).)
152. Which operator has the highest precedence in PHP expressions?
A) +
B) *
C) =
D) ()
Answer: D) ()
(Parentheses have the highest precedence in PHP expressions.)
153. What will the following expression evaluate to?
php
CopyEdit
$x = 7;
$y = 3;
$result = $x / $y;
echo $result;
A) 2
B) 2.3333
C) 3
D) 0
Answer: B) 2.3333
(Division in PHP returns a float if the result is not an integer.)
154. What will be the result of the following expression?
php
CopyEdit
$x = 10;
$y = 5;
$z = $x % $y;
echo $z;
A) 2
B) 5
C) 10
D) 0
Answer: D) 0
(The modulo operator returns the remainder of the division, and 10 % 5 = 0.)
155. Which of the following expressions will return true in PHP?
A) 5 > 10
B) 10 == "10"
C) "hello" == "world"
D) 5 != 5
Answer: B) 10 == "10"
(PHP compares values loosely and 10 == "10" is true because the string "10" is converted to the
integer 10.)
156. Which tag is used to start PHP code in a file?
A) <script>
B) <php>
C) <?php
D) <?=
Answer: C) <?php
157. What is the correct file extension for a PHP file?
A) .html
B) .php
C) .xml
D) .txt
Answer: B) .php
158. Which command can be used to run a PHP script on the command line?
A) run file.php
B) php file.php
C) execute file.php
D) launch file.php
Answer: B) php file.php
159. What is the output of the following PHP code?
php
CopyEdit
<?php
echo "Hello, World!";
?>
A) Error
B) No output
C) Hello, World!
D) echo
Answer: C) Hello, World!
160. Where should PHP code be placed in an HTML file?
A) Outside the <body> tag
B) Inside a <php> tag
C) Inside <?php ... ?> tags
D) Only at the top of the file
Answer: C) Inside <?php ... ?> tags
161. Which of the following is required to run PHP code in a browser?
A) MySQL
B) PHP compiler
C) Web server like Apache or Nginx
D) HTML5
Answer: C) Web server like Apache or Nginx
162. What will be the output of this PHP code in a .php file run via a web
browser?
php
CopyEdit
<?php
echo 2 + 3 * 4;
?>
A) 20
B) 14
C) 24
D) 9
Answer: B) 14
(Multiplication has higher precedence, so 3 * 4 = 12, then 2 + 12 = 14.)
163. Which software is commonly used to run a local PHP server?
A) WAMP
B) XAMPP
C) LAMP
D) All of the above
Answer: D) All of the above
164. What URL is typically used to access PHP files when using a local server
like XAMPP or WAMP?
A) localhost/filename.php
B) server.local/filename.php
C) php://localhost
D) myhost.php
Answer: A) localhost/filename.php
165. Which function is used to print text to the browser in PHP?
A) write()
B) print()
C) show()
D) display()
Answer: B) print()
(Or also echo(), which is more commonly used.)