Which of the following are common uses of Regular Expressions in PHP?
- Validating user input, such as email addresses or phone numbers.
- Performing string substitutions or manipulations.
- Parsing and extracting data from text, such as log files or web scraping.
- Searching and filtering text data based on specific patterns.
Common uses of Regular Expressions in PHP include validating user input, such as email addresses or phone numbers, by checking if they match the required pattern. Regular Expressions are also used for performing string substitutions or manipulations, allowing you to search for specific patterns in a string and replace them with desired values. They are useful in parsing and extracting data from text, such as log files or web scraping, as they can match and extract specific patterns. Additionally, Regular Expressions are employed in searching and filtering text data based on specific patterns, providing a powerful tool for data manipulation and analysis. Learn more: https://www.php.net/manual/en/book.regex.php
You have a floating-point number in your PHP script and you need to round it to the nearest integer. How would you do this?
- round($number)
- ceil($number)
- floor($number)
- intval($number)
To round a floating-point number to the nearest integer in PHP, you can use the round() function. The round() function rounds a floating-point number to the nearest whole number. It accepts a single argument, the number to be rounded, and returns the rounded value. The rounding behavior can be modified by specifying the optional precision parameter. This function is useful when you need to round a floating-point number to the nearest integer. Learn more: https://www.php.net/manual/en/function.round.php
PHP constants are case-_________.
- Insensitive
- Sensitive
- Dependent
- Independent
PHP constants are case-sensitive. It means that constant names are treated as case-sensitive identifiers. For example, if a constant is defined as "CONSTANT_NAME", you cannot access it as "constant_name" or "CoNsTaNt_NaMe". The constant name must match exactly with its defined case. This behavior ensures that constants are accessed consistently based on their exact names. Learn more: https://www.php.net/manual/en/language.constants.php
What is the difference between Exception::getMessage and Exception::getLine?
- getMessage returns the error message associated with the exception, while getLine returns the line number where the exception occurred
- getMessage returns the line number where the exception occurred, while getLine returns the error message associated with the exception
- They both return the same information
- They are not valid methods in the Exception class
Exception::getMessage returns the error message associated with the exception, while Exception::getLine returns the line number where the exception occurred. They provide different information about the exception. Learn more: http://php.net/manual/en/class.exception.php
The keys in a PHP associative array can be both strings and integers.
- TRUE
- FALSE
True. In a PHP associative array, the keys can be both strings and integers. You can explicitly assign either string or integer keys to the elements of an associative array. This flexibility allows you to associate specific values with meaningful labels or identifiers. You can access the corresponding values in the array using the associated keys. Associative arrays are widely used in PHP for organizing and retrieving data in a non-sequential manner. Learn more: https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax
What is an interface in the context of PHP OOP?
- A contract for
- An abstract class
- A concrete class
- A trait
In PHP OOP, an interface is indeed a contract or a set of rules that defines a specific behavior or functionality. It provides a way to establish a common structure and ensure that classes that implement the interface adhere to that structure. An interface contains only method signatures without implementation. Classes that implement an interface must provide an implementation for all the methods defined in the interface. Interfaces allow for polymorphism and provide a way to define a common interface that multiple classes can adhere to. For further information, visit: http://php.net/manual/en/language.oop5.interfaces.php
A common use case of the include statement in PHP is to include ______.
- reusable code
- database connections
- external APIs
- CSS stylesheets
One of the common use cases of the include statement in PHP is to include reusable code from other files. This allows you to organize your code into separate files and include them as needed, reducing redundancy and promoting code reuse.
What is the meaning of a Persistent Cookie?
- A cookie that never expires
- A cookie that is always secure
- A cookie that is shared across pages
- A cookie that is permanent
A persistent cookie is a type of cookie that is stored on the user's device even after they close their browser. It has an expiration date set in the future. Learn more: http://php.net/manual/en/function.setcookie.php
You are tasked with creating a PHP function that accepts a filename, opens the file, prints its contents, and then closes the file. How would you approach this task?
- Use the fopen() function to open the file, read its contents using fread() or other file reading functions, print the contents, and then close the file using fclose()
- Use the readfile() function to directly output the file contents, and then close the file using fclose()
- Use the file_get_contents() function to read the entire file into a string, print the contents, and then close the file using fclose()
- Use the file() function to read the file line by line into an array, print the contents, and then close the file using fclose()
You can create a PHP function that accepts a filename. Inside the function, you would use fopen() to open the file, fread() or other file reading functions to read its contents, print the contents as desired, and then close the file using fclose(). This ensures proper file handling and cleanup after printing the contents.
What PHP function can be used to write to a file?
- readfile()
- file_get_contents()
- fwrite()
- fopen()
The fwrite() function in PHP is used to write to a file. It takes the file handle obtained from fopen() as the first argument and the content to write as the second argument. It returns the number of bytes written or false on failure.