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.
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.
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.
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().
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.
The ________ operator in PHP is used to check if two values are not equal.
- != (Not Equal)
- == (Equal)
- >= (Greater Than or Equal)
- !== (Identically Not Equal)
In PHP, the '!=' operator checks for inequality between two values. It returns true if the values are not equal.
The PHP function used to split an array into chunks of new arrays with specified size is ________.
- array_chunk
- array_split
- array_divide
- array_segment
The array_chunk function in PHP divides an array into chunks of specified size. It's useful for tasks like pagination, where you split data into manageable portions.
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.
To get the ASCII value of the first character of a string in PHP, you would use the ________ function.
- ord()
- ascii()
- charCode()
- getASCII()
The 'ord()' function in PHP is used to obtain the ASCII value of the first character of a string. It's handy for character-level operations.
In a form, what should the enctype attribute be set to for uploading files?
- text/plain
- application/json
- multipart/form-data
- application/x-www-form-urlencoded
The enctype attribute in an HTML form should be set to multipart/form-data when you want to allow file uploads. This encoding type is used for sending binary data, like files.
The PHP function ________ is used to repeat a string a specified number of times.
- str_repeat()
- repeat_string()
- repeat()
- multiply_string()
The str_repeat() function in PHP is used to repeat a string a specified number of times. It's a helpful way to create repeated patterns or sequences.
For how long does a session variable persist by default in PHP?
- Until the browser is closed
- Until the server restarts
- For 24 hours
- Until the session ends
By default, session variables in PHP persist until the session ends. This typically happens when the user closes the browser or remains inactive for a set period, often 24 minutes.