PHP Practice Test — Questions and Answers
Question 1: What is the meaning of PHP?
- Personal Hypertext Processor
- Private Home Page
- PHP: Hypertext Preprocessor (Correct answer)
Correct answer: PHP: Hypertext Preprocessor
PHP originally stood for 'Personal Home Page.' However, it was later officially redefined as a recursive acronym, 'PHP: Hypertext Preprocessor.' This new definition better reflects its primary role as a server-side scripting language for web development.
Question 2: Which of the following methods for declaring a PHP variable is incorrect?
- $AVariable;
- $aVariable;
- $a Variable; (Correct answer)
- $a_Variable = 9;
Correct answer: $a Variable;
In PHP, variable names must begin with a dollar sign ($) followed by a letter or an underscore, and can only contain alphanumeric characters and underscores. Spaces are not permitted within a variable name, making '$a Variable;' an incorrect and invalid method for declaring a PHP variable.
Question 3: What is the proper method for defining constants in PHP?
- Const $var
- Constant $var
- Define("Constant"); (Correct answer)
- Constant ($var)
Correct answer: Define("Constant");
In PHP, constants are defined using the `define()` function. This function takes two main arguments: the name of the constant as a string, and its value. Unlike variables, constants do not start with a dollar sign and cannot be changed once declared, making them useful for fixed values like database credentials or application settings.
Question 4: What are the delimiters that surround PHP server scripts?
- <&>...</&>
- <?php>...</?>
- <script>...</script>
- <?php...?> (Correct answer)
Correct answer: <?php...?>
PHP code blocks are typically enclosed within the `<?php` and `?>` delimiters. These tags tell the web server to interpret the enclosed code as PHP, rather than plain HTML. While shorter tags like `<?` and `?>` exist, `<?php` is the recommended and most widely compatible standard for marking PHP script sections.
Question 5: Which of the following methods for declaring a PHP variable is incorrect?
- $a_Number = 9;
- $a_Number = 9 (Correct answer)
- $aNumber = 9;
- $a_Num = 9;
Correct answer: $a_Number = 9
PHP statements, including variable declarations, must end with a semicolon (`;`). Option B `$a_Number = 9` is missing this crucial terminator, which would result in a parse error. The other options correctly declare a variable and assign a value, terminating the statement with a semicolon.
Question 6: Which function will you use to quickly transform an HTML page into a database-friendly format?
- Htmlentities() (Correct answer)
- Htmlspecialchars()
- Stripslashes()
Correct answer: Htmlentities()
The `htmlentities()` function converts all applicable characters to HTML entities, making the data safe to store in a database and display on a webpage without rendering malicious HTML or breaking the page layout. This is crucial for preventing cross-site scripting (XSS) attacks when user-supplied data is involved. While `htmlspecialchars()` converts only a subset of characters, `htmlentities()` is more comprehensive for full HTML conversion.
Question 7: In PHP, how do you type "Hello World"?
- "Hello World";
- echo "Hello World"; (Correct answer)
- Document.Write("Hello World");
Correct answer: echo "Hello World";
In PHP, the `echo` statement is used to output strings, numbers, or other data to the browser. It is a language construct, not a function, and is commonly used for displaying content. The other options are either incorrect syntax for PHP or belong to other languages.
Question 8: Is it true/false that PHP variables are case sensitive?
- A) False
- B) True (Correct answer)
Correct answer: B) True
PHP variables are indeed case-sensitive. This means that `$myVariable` and `$myvariable` are treated as two distinct variables. It's important to maintain consistent casing when referencing variables to avoid errors and ensure your code behaves as expected.
Question 9: To create a CURL session, which of the following functions will be used?
- Curl_init() (Correct answer)
- Curl_exec()
- Curl_setopt()
- Curl_opt()
Correct answer: Curl_init()
The `curl_init()` function is used to initialize a new cURL session and returns a cURL handle. This handle is then used by other cURL functions, such as `curl_setopt()` to set options and `curl_exec()` to execute the session. It's the essential first step for making HTTP requests with cURL in PHP.
Question 10: Which symbol is used to begin all variables in PHP?
- &
- !
- $ (Correct answer)
Correct answer: $
In PHP, all user-defined variables must start with a dollar sign (`$`). This symbol distinguishes variables from other language constructs and keywords. It's a fundamental rule of PHP variable syntax that helps the interpreter identify variables.
Question 11: What data type will PHP convert the following variable to automatically:? $aVariable = 99;
- A) String (a text variable)
- B) Integer (a number variable) (Correct answer)
Correct answer: B) Integer (a number variable)
PHP is a loosely typed language, meaning it automatically determines the data type of a variable based on the value assigned to it. Since `99` is a whole number, PHP will automatically interpret `$aVariable` as an integer. This automatic conversion is known as type juggling.
Question 12: To get a SINGLE record from a MySql resultset, which of the following statements will be used?
- Mysql_fetch_array
- Mysql_connect
- Mysql_fetch_row (Correct answer)
- Mysql_query
Correct answer: Mysql_fetch_row
The `mysql_fetch_row()` function (from the deprecated `mysql` extension) retrieves a single row from a MySQL result set as a numerically indexed array. Each call to this function fetches the next row until no more rows are available. For modern PHP, `mysqli_fetch_row()` or `PDOStatement::fetch(PDO::FETCH_NUM)` would be used.
Question 13: Is it true/false that once a variable is declared, it can only be used once in the PHP source code?
- A) False (Correct answer)
- B) True
Correct answer: A) False
This statement is false. Variables are designed to be reused multiple times throughout a program's execution. Once declared and assigned a value, a variable can be accessed, modified, and used in various operations as many times as needed within its scope, making code dynamic and efficient.
Question 14: What is the correct MySql database connection syntax?
- Connect_mysql($username,$password)
- Mysql_connect("localhost",$username,$password) (Correct answer)
- Mysql_connect($username,$password)
Correct answer: Mysql_connect("localhost",$username,$password)
The `mysql_connect()` function (now deprecated) was used to establish a connection to a MySQL database server. The correct syntax requires specifying the hostname (often "localhost"), followed by the username and password for the database. Modern PHP applications should use `mysqli_connect()` or PDO for database connections due to security and feature improvements.
Question 15: The syntax of PHP is most similar to:
- JavaScript
- VBScript
- Perl and C (Correct answer)
Correct answer: Perl and C
PHP's syntax draws heavily from C, particularly its control structures (if, for, while) and function definitions. It also shares similarities with Perl, especially in its variable naming conventions (e.g., dollar sign for variables) and string manipulation capabilities. This makes it relatively familiar to developers with backgrounds in these languages.
Question 16: Isn't it true/false that all variables in PHP begin with a $(dollar) sign?
- A) False
- B) True (Correct answer)
Correct answer: B) True
This statement is true. A fundamental rule in PHP is that all user-defined variables must be prefixed with a dollar sign (`$`). This convention helps distinguish variables from other language elements like functions, constants, or keywords, making them easily identifiable within the code.
Question 17: After using the mysql connect() method, which of the following functions must be called?
- Mysql_select_db (Correct answer)
- Mysql_query
- Mysql_fetch_row
- Mysql_fetch_array
Correct answer: Mysql_select_db
After establishing a connection to the MySQL server using `mysql_connect()`, it was necessary to select the specific database to work with using `mysql_select_db()`. This function tells PHP which database to perform subsequent queries on. For modern PHP, `mysqli_select_db()` or specifying the database name directly in `mysqli_connect()` or PDO connection string is used.
Question 18: Variables appear in the URL when using the POST method:
- A) False (Correct answer)
- B) True
Correct answer: A) False
When using the POST method to submit form data, variables are sent in the HTTP request body, not in the URL. This makes POST suitable for sending sensitive information or large amounts of data, as it's not visible in the browser's address bar or stored in browser history. The GET method, conversely, appends variables to the URL.
Question 19: Select the best appropriate statement to verify that a SELECT SQL query was successfully executed.
- If (mysqli_query($link, "SELECT * FROM keyContacts")) (Correct answer)
- $link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());
- $sql = "SELECT * FROM keyContacts";
- If ($row=mysqli_fetch_assoc($result))
Correct answer: If (mysqli_query($link, "SELECT * FROM keyContacts"))
The `mysqli_query()` function returns `FALSE` on failure for `SELECT`, `SHOW`, `DESCRIBE` or `EXPLAIN` queries, and a `mysqli_result` object on success. Therefore, checking if `mysqli_query()` returns a truthy value (which a `mysqli_result` object is) is the correct way to verify successful execution of a SELECT query. This allows you to proceed with fetching results or handle errors appropriately.
Question 20: PHP 5.3.0 introduces a new functionality that allows you to refer to a called class in a static inheritance context. What's the name of it?
- Late static bindings (Correct answer)
- Static class bindings
- Class bindings
- Static bindings
Correct answer: Late static bindings
Late static bindings, introduced in PHP 5.3.0, provide a way to refer to the called class in a static context. This allows for more flexible static method and property inheritance, as `static::` refers to the class that was originally called at runtime, rather than the class where the method was defined. It addresses limitations of `self::` and `parent::` in inheritance hierarchies.
Question 21: The extension ".inc" is required for include files.
- A) True
- B) False (Correct answer)
Correct answer: B) False
This statement is false. While `.inc` is a common convention for include files, PHP does not enforce any specific file extension for included files. You can use `.php`, `.html`, or even no extension at all, as long as the server is configured to process the file correctly if it contains PHP code.
Question 22: The circular reference collector is activated by which function?
- Gc_enable() (Correct answer)
- Gc_activate()
- Gc_rc()
- Crc_activate()
Correct answer: Gc_enable()
The `gc_enable()` function activates PHP's circular reference collector. This garbage collector is responsible for detecting and freeing memory occupied by objects that form circular references, which the simple reference counting mechanism cannot handle. Enabling it helps manage memory more efficiently in complex applications by preventing memory leaks.
Question 23: Is it possible to create functions without giving their names using anonymous functions?
- There is nothing like 'anonymous functions'
- Yes (Correct answer)
- No
Correct answer: Yes
Yes, it is possible to create functions without names in PHP using anonymous functions, also known as closures. These functions can be assigned to variables, passed as arguments to other functions, or returned from functions, providing flexibility for callback functions and event handlers. They were introduced in PHP 5.3.
Question 24: How do you add a comment in PHP?
- <comment>...</comment>
- *\...\*
- /*...*/ (Correct answer)
- <!--...-->
Correct answer: /*...*/
PHP supports several ways to add comments to your code. `/* ... */` is used for multi-line comments, allowing you to comment out blocks of code or extensive explanations. Single-line comments can be made using `//` or `#` at the beginning of a line or after a statement.
Question 25: Is it possible to run PHP from the command line?
- No
- Yes (Correct answer)
- Only on Windows
- Only on linux
Correct answer: Yes
Yes, PHP can be run from the command line using the PHP CLI (Command Line Interface) SAPI (Server Application Programming Interface). This allows developers to execute PHP scripts without a web server, making it useful for cron jobs, scripting tasks, and testing. You simply invoke `php your_script.php` in your terminal.
What is the meaning of PHP?