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.

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.

The base class for all exceptions in PHP is ________.

  • Exception
  • Throwable
  • Error
  • RuntimeException
In PHP, the base class for all exceptions is 'Throwable.' It serves as the base class for both exceptions and errors.

How can you include a file from another namespace without using its fully qualified name?

  • use statement
  • include statement
  • require statement
  • namespace keyword
You can include a file from another namespace without using its fully qualified name by using the use statement. It allows you to alias namespaces or import classes/functions into the current namespace.

Which of the following best describes "defensive programming" in the context of PHP?

  • Writing code that anticipates and handles errors
  • Writing code that aggressively catches and suppresses all errors
  • Writing code that relies on system error messages to identify issues
  • Writing code that creates intentional vulnerabilities for testing purposes
Defensive programming in PHP involves writing code that anticipates and handles errors, aiming to make the code robust and resilient to unexpected issues.

Which of the following is the correct way to define an associative array in PHP?

  • $colors = ['red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF']
  • $colors = ['red', 'green', 'blue']
  • $colors = ('red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF')
  • $colors = {'red' => '#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF'}
The correct way to define an associative array in PHP is by using the format shown in Option 1, where keys are associated with values using the => symbol.

To ensure that an email field contains a valid email address, you can use the PHP ________ filter.

  • sanitize_email()
  • validate_email()
  • filter_var()
  • check_email()
You can use the PHP filter_var() function with the FILTER_VALIDATE_EMAIL filter to ensure that an email field contains a valid email address.

PHP is loosely typed, meaning:

  • Data types are strictly enforced
  • Data types are dynamically determined
  • Data types are not used in PHP
  • Data types are implicitly cast
PHP is loosely typed, meaning that data types are dynamically determined by the context in which they are used. Variables can change their data type as needed.

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.

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.

What is the primary purpose of a constructor method in a PHP class?

  • Initializing variables
  • Defining class methods
  • Creating new objects
  • Destructing the object
The primary purpose of a constructor method in a PHP class is to initialize the class's properties or variables when an object is created. It's automatically called when an object is instantiated.

Imagine you're building a multi-language website. Which method would be best to remember the user's language preference over multiple visits?

  • Cookies
  • Local Storage
  • Session Storage
  • Browser Cookies
To remember the user's language preference over multiple visits, "Cookies" are the best method. They can persist data across sessions and are widely used for such purposes.