Computer

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

Answer
c) PHP

Which function is used to print output in PHP?
a) echo
b) print
c) printf
d) All of the above

Answer
d) All of the above

Which symbol is used to declare a PHP variable?
a) &
b) %
c) $
d) #

Answer
c) $

What is the default file extension for a PHP file?
a) .html
b) .php
c) .js
d) .css

Answer
b) .php

Which function is used to connect to a MySQL database in PHP?
a) mysql_connect()
b) mysqli_connect()
c) connect_db()
d) db_connect()

Answer
b) mysqli_connect()

Which of the following is used to start a PHP session?
a) start_session()
b) session_start()
c) session_open()
d) open_session()

Answer
b) session_start()

Which superglobal variable is used to collect form data sent using the POST method?
a) $_GET
b) $_POST
c) $_REQUEST
d) $_SESSION

Answer
b) $_POST

Which PHP function is used to check if a file exists?
a) check_file()
b) file_exist()
c) file_exists()
d) is_file()

Answer
c) file_exists()

Which function is used to redirect a user in PHP?
a) redirect()
b) header()
c) go_to()
d) location()

Answer
b) header()

Which of the following is the correct way to start a PHP script?
a) <?php
b) <?
c) <script>
d) <php>

Answer
a) <?php

Which function is used to terminate a PHP script?
a) stop()
b) exit()
c) die()
d) Both b and c

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

Answer
b) setcookie(“name”, “value”, time() + 3600);

Which function is used to include a PHP file?
a) import()
b) include()
c) require()
d) Both b and c

Answer
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

Answer
b) Splits a string into an array

Which function is used to sanitize user input in PHP?
a) htmlspecialchars()
b) htmlentities()
c) strip_tags()
d) All of the above

Answer
d) All of the above

Which of the following is NOT a valid PHP data type?
a) String
b) Boolean
c) Float
d) Character

Answer
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

Answer
b) To check if a variable is set and not null

Which function is used to get the length of a string in PHP?
a) count()
b) strlen()
c) length()
d) size()

Answer
b) strlen()

Which function is used to send an email in PHP?
a) mail()
b) sendmail()
c) email_send()
d) smtp_mail()

Answer
a) mail()

Which function is used to generate a random number in PHP?
a) random()
b) rand()
c) generate_random()
d) mt_rand()

Answer
b) rand()

How do you comment a single line in PHP?
a) /* Comment */
b) // Comment
c) # Comment
d) Both b and c

Answer
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

Answer
a) Checks if a variable has no value

Which operator is used for string concatenation in PHP?
a) +
b) .
c) &
d) %

Answer
b) .

Which function in PHP is used to delete a file?
a) delete()
b) remove()
c) unlink()
d) erase()

Answer
c) unlink()

Which function is used to get the current date and time in PHP?
a) date()
b) time()
c) now()
d) datetime()

Answer
a) date()

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

Answer
b) Converts an array to JSON

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

Answer
b) Includes a file only once

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

Answer
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

Answer
b) The current script filename

Which function is used to stop a script execution in PHP?
a) stop()
b) exit()
c) break()
d) end()

Answer
b) exit()

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button