PHP & Server-Side Scripting MCQs with Answers
Which of the following is a server-side scripting language?
a) HTML
b) CSS
c) PHP
d) JavaScript
Which function is used to print output in PHP?
a) echo
b) print
c) printf
d) All of the above
Which symbol is used to declare a PHP variable?
a) &
b) %
c) $
d) #
What is the default file extension for a PHP file?
a) .html
b) .php
c) .js
d) .css
Which function is used to connect to a MySQL database in PHP?
a) mysql_connect()
b) mysqli_connect()
c) connect_db()
d) db_connect()
Which of the following is used to start a PHP session?
a) start_session()
b) session_start()
c) session_open()
d) open_session()
Which superglobal variable is used to collect form data sent using the POST method?
a) $_GET
b) $_POST
c) $_REQUEST
d) $_SESSION
Which PHP function is used to check if a file exists?
a) check_file()
b) file_exist()
c) file_exists()
d) is_file()
Which function is used to redirect a user in PHP?
a) redirect()
b) header()
c) go_to()
d) location()
Which of the following is the correct way to start a PHP script?
a) <?php
b) <?
c) <script>
d) <php>
Which function is used to terminate a PHP script?
a) stop()
b) exit()
c) die()
d) Both b and c
What is the correct way to create a cookie in PHP?
a) cookie_set(“name”, “value”);
b) setcookie(“name”, “value”, time() + 3600);
c) create_cookie(“name”, “value”);
d) set_cookie(“name”, “value”);
Which function is used to include a PHP file?
a) import()
b) include()
c) require()
d) Both b and c
What does the explode() function do in PHP?
a) Combines strings into an array
b) Splits a string into an array
c) Deletes a string
d) Converts an array to a string
Which function is used to sanitize user input in PHP?
a) htmlspecialchars()
b) htmlentities()
c) strip_tags()
d) All of the above
Which of the following is NOT a valid PHP data type?
a) String
b) Boolean
c) Float
d) Character
What is the purpose of the isset() function?
a) To check if a variable is empty
b) To check if a variable is set and not null
c) To delete a variable
d) To initialize a variable
Which function is used to get the length of a string in PHP?
a) count()
b) strlen()
c) length()
d) size()
Which function is used to send an email in PHP?
a) mail()
b) sendmail()
c) email_send()
d) smtp_mail()
Which function is used to generate a random number in PHP?
a) random()
b) rand()
c) generate_random()
d) mt_rand()
How do you comment a single line in PHP?
a) /* Comment */
b) // Comment
c) # Comment
d) Both b and c
What does empty() do in PHP?
a) Checks if a variable has no value
b) Deletes a variable
c) Initializes a variable
d) Converts a variable to null
Which operator is used for string concatenation in PHP?
a) +
b) .
c) &
d) %
Which function in PHP is used to delete a file?
a) delete()
b) remove()
c) unlink()
d) erase()
Which function is used to get the current date and time in PHP?
a) date()
b) time()
c) now()
d) datetime()
What does json_encode() do in PHP?
a) Converts JSON to an array
b) Converts an array to JSON
c) Encrypts data
d) Decodes a JSON string
What is the purpose of require_once()?
a) Includes a file multiple times
b) Includes a file only once
c) Checks if a file exists
d) Deletes a file
Which PHP function is used to count the number of elements in an array?
a) count()
b) sizeof()
c) array_count()
d) Both a and b
What will $_SERVER[‘PHP_SELF’] return?
a) The server IP address
b) The current script filename
c) The PHP version
d) The current session ID
Which function is used to stop a script execution in PHP?
a) stop()
b) exit()
c) break()
d) end()