In PHP, which keyword is used to define a constant?
- constant
- define
- const
- let
Constants in PHP are defined using the define function. It's not a keyword but a function used to create named constants with a specific value.
You are building a web application where the security of user data is paramount. Which password hashing algorithm would be most appropriate to use in PHP?
- password_hash()
- md5()
- sha1()
- bcrypt()
bcrypt() is the recommended choice for secure password hashing in PHP. It's a one-way hashing algorithm with salting, making it extremely resistant to brute-force and rainbow table attacks. In contrast, md5() and sha1() are outdated and less secure. password_hash() is a more recent option, but it's best used with bcrypt().
A variable that starts with a number in PHP will result in a ________.
- Syntax Error
- Warning
- Notice
- No Error
In PHP, a variable that starts with a number will result in a Syntax Error. Variable names must start with a letter or underscore in PHP.
In a multidimensional array, how would you access the second element of the first array?
- $array[0][1]
- $array[1][0]
- $array[2][1]
- $array[0][2]
In a multidimensional array, you access elements by specifying the indices for each dimension. To access the second element of the first array, you would use $array[0][1], where 0 represents the first array and 1 represents the second element within that array.
Which of the following is a predefined function in PHP used to get the length of a string?
- str_length()
- strlen()
- string_length()
- lengthOfString()
The 'strlen()' function in PHP is used to find the length of a string, counting the number of characters in the string. It's a commonly used function for string manipulation.
When a child class inherits from a parent class, it can also be referred to as a ________ relationship.
- Inheritance
- Association
- Aggregation
- Composition
In object-oriented programming, when a child class inherits from a parent class, it forms an inheritance relationship, inheriting properties and methods.
Which of the following PHP functions is commonly used to create a hashed password?
- encrypt()
- hash_password()
- password_hash()
- create_hashed_password()
The password_hash() function in PHP is commonly used for creating hashed passwords. It incorporates strong encryption algorithms and automatically generates a unique salt for each password, enhancing security.
When using PDO, which method is used to prepare a statement for execution?
- prepare()
- execute()
- query()
- bindParam()
The prepare() method in PDO is used to prepare an SQL statement for execution. It's typically used with placeholders to prevent SQL injection and to improve query performance by reusing the prepared statement with different parameter values.
The ________ function in PHP can be used to regenerate a session ID.
- session_regenerate_cookie
- session_regenerate_id
- session_reset_id
- session_destroy
The correct option is session_regenerate_id. It regenerates the session ID and helps enhance session security.
Consider a scenario where you are designing a survey form where questions can be dynamically added by the admin. How would you design the form fields to ensure all data is captured correctly in PHP?
- Use an array of form fields
- Use session variables
- Use cookies
- Use hidden fields
To ensure that all dynamically added questions are captured correctly in PHP, it's advisable to use an array of form fields. Each dynamically added question can have its own field within the array.