Which PHP superglobal contains information about headers, paths, and script locations?
- $_SERVER
- $_ENV
- $_GLOBALS
- $_LOCALS
The $_SERVER superglobal in PHP contains an array of information about the server environment, including headers, paths, and script locations. It's often used for server-related tasks.
Which of the following PHP functions checks if a file or directory exists?
- file_exists()
- include_once()
- require_once()
- class_exists()
The file_exists() function in PHP is used to determine if a file or directory exists. It returns a boolean value, true if the file or directory exists and false if it doesn't.
You have a string "OpenAI" and you want to replace "Open" with "Close". Which PHP function will you use?
- str_replace("Open", "Close", $str)
- str_replace("Close", "Open", $str)
- str_concat("Open", "Close", $str)
- str_split("Open", "Close", $str)
To replace a substring in a string, you should use the str_replace function in PHP. It replaces all occurrences of the specified substring with another string within the given string.
What is the output of the following code? echo count(array("apple", "banana", "cherry"));
- 3
- 6
- 0
- 1
The count function in PHP returns the number of elements in an array. In this case, it counts the elements in the given array, which is 3. So, the output will be 3.
Which of the following is a common technique used to prevent cross-site scripting (XSS) attacks?
- Input validation
- Using strong passwords
- Escaping user-generated content
- Storing user data in plain text format
To prevent cross-site scripting (XSS) attacks, it's common to escape user-generated content before rendering it in web pages. This prevents code execution.
You are auditing a web application and notice that the session IDs are predictable and sequential. Why might this be a security concern?
- Session fixation attack
- XSS attack
- CSRF attack
- SQL injection attack
Predictable and sequential session IDs make the application vulnerable to a session fixation attack, where an attacker can set a user's session ID. This poses a significant security risk. Other attacks like XSS, CSRF, and SQL injection are unrelated to session ID predictability.
How can you pass a variable by reference to a function in PHP?
- Using the '&' symbol before the variable
- Using the 'ref' keyword before the variable
- Prefixing the variable with a '#' symbol
- Using the '*' symbol before the variable
To pass a variable by reference in PHP, you use the '&' symbol before the variable name. This allows changes made to the variable within the function to affect the original variable outside the function. The other options are incorrect.
You are given a task to perform different actions based on the day of the week. Which control structure is most appropriate for this scenario?
- If-Else
- Switch-Case
- While Loop
- For Loop
A 'Switch-Case' control structure is ideal when you have multiple conditions to evaluate based on the value of a single variable, such as days of the week.
Which predefined PHP function is used to find the position of the first occurrence of a substring?
- strpos($string, $substring)
- strfind($string, $substring)
- strstrpos($string, $substring)
- locate($substring, $string)
The correct option is 'strpos($string, $substring)'. It is used to find the position of the first occurrence of a substring within a string. The other options are incorrect functions.
How can you remove the first element from an array in PHP?
- array_shift()
- array_pop()
- unset()
- array_slice($array, 1)
The array_shift() function removes and returns the first element of an array, effectively shifting the array's keys.