You have a long string containing multiple words and you need to extract each word as an individual item in an array. Which PHP function would be most suitable for this task?
- explode()
- implode()
- split()
- preg_split()
The explode() function in PHP is used to split a string into an array based on a specified delimiter. This is ideal for separating words in a long string into individual array items.
In PDO, the placeholders used in prepared statements can be either named or ________.
- Indexed
- Positional
- Indexed or Positional
- Named
In PDO, placeholders in prepared statements can be either named (e.g., :name) or positional (e.g., ?). Both are valid ways to bind values.
What is the main difference between the strcmp() and strcasecmp() functions in PHP?
- strcmp() compares case
- strcmp() is for binary
- strcmp() returns 0 on
- strcmp() is for
The main difference is that strcmp() is case-sensitive and returns 0 when two strings are equal, while strcasecmp() is case-insensitive and still returns 0 on equality.
Why is it recommended to use prepared statements in PHP when interacting with a database?
- To improve code readability.
- To prevent SQL injection attacks.
- To reduce the need for database indexes.
- To enhance database schema design.
Prepared statements prevent SQL injection attacks by separating SQL code from user input, making it safer to execute database queries.
When an error is not caught in PHP, it results in a ________ error.
- Fatal
- Syntax
- Logical
- Run-time
When an error is not caught in PHP, it results in a 'Fatal' error, which typically terminates script execution. Fatal errors often include issues like calling an undefined function or division by zero.
Which of the following is NOT a recommended approach to prevent SQL injection attacks in PHP?
- Using Prepared Statements
- Validating and Sanitizing User Input
- Escaping User Input with mysqli_real_escape_string()
- Disabling Error Reporting
Validating and sanitizing user input is an essential step in preventing SQL injection attacks. Therefore, it is not recommended to avoid this step. Other options, like using prepared statements and escaping user input, are recommended practices to prevent SQL injection in PHP. Disabling error reporting, while helpful, is not the primary method for prevention.
When you don't want to handle an exception and want it to be handled by a parent catch block, you can use the ________ keyword.
- rethrow
- propagate
- throwup
- throw
The throw keyword is used to rethrow an exception that you don't want to handle in the current catch block, allowing it to be caught by a parent catch block.
To append one or more elements to the end of an array, the ________ function is used.
- array_push
- array_merge
- array_append
- array_combine
The array_push function is used to append one or more elements to the end of an array in PHP. It's a way to add elements to an existing array.
The PHP function that merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one is ________.
- array_merge
- array_combine
- array_push
- array_merge_recursive
The correct function is 'array_merge.' This function takes multiple arrays and combines them, appending the values of one array to the end of the previous one.
How do you define a namespace in a PHP script?
- Using the "namespace" keyword
- Wrapping code in curly braces
- Specifying it in the file's name
- Using the "define_namespace" keyword
In PHP, you define a namespace using the "namespace" keyword, followed by the namespace name. This helps encapsulate your code into a specific namespace.