You need to access server-specific information in your PHP script. How would you do this using the $_SERVER superglobal?
- Access the desired element of the $_SERVER array using the appropriate key.
- Use the $_SERVER superglobal as an argument to a function.
- Assign the value of the desired element to a local variable.
- Iterate over the elements of the $_SERVER array using a loop.
To access server-specific information using the $_SERVER superglobal in PHP, you can directly access the desired element of the $_SERVER array using the appropriate key. The $_SERVER superglobal is an associative array that contains various server-specific information, such as headers, paths, script locations, server details, and more. By accessing the specific key within the $_SERVER array, you can retrieve the server-specific information needed in your PHP script. Learn more: https://www.php.net/manual/en/reserved.variables.server.php
A static method in PHP OOP is a method that belongs to the class itself rather than an instance of the class. It can be called without creating an ______ of the class.
- Object
- Instance
- Reference
- Array
A static method in PHP OOP can be called without creating an instance of the class. Since it belongs to the class itself, it can be accessed using the class name directly, without the need to instantiate an object.
You are writing a PHP script and you need to sanitize user input. How would you do this?
- filter_input()
- sanitize_input()
- validate_input()
- clean_input()
To sanitize user input in PHP, you can use the filter_input() function. This function allows you to filter and sanitize user input based on predefined filters or custom filters. It provides a convenient way to ensure that the input is safe and free from unwanted content. For more information, refer to: http://php.net/manual/en/function.filter-input.php
How can you retrieve the value of a specific cookie in PHP?
- Using the $_COOKIE superglobal array
- Using the $_SESSION superglobal array
- Using the $_GET superglobal array
- Using the $_SERVER superglobal array
The value of a specific cookie in PHP can be retrieved using the $_COOKIE superglobal array. This array contains all the cookies sent by the client, and you can access the value of a specific cookie by specifying its name as the array index. Learn more: http://php.net/manual/en/reserved.variables.cookies.php
You need to close a file in your PHP script after you're done with it. How would you do this?
- close()
- fclose()
- end()
- finish()
To close a file in a PHP script, you would use the fclose() function. It takes the file handle obtained from fopen() as its parameter. This function releases the resources associated with the file and frees up memory. It is a good practice to close files after you have finished working with them to ensure proper cleanup and prevent resource leaks.
What are some common uses of the $_SESSION superglobal array in PHP?
- Storing user data
- Tracking user activity
- Implementing shopping carts
- Maintaining user preferences
- All the options
The $_SESSION superglobal array in PHP is commonly used for various purposes. It allows storing user-specific data, tracking user activity across different pages, implementing shopping carts, and maintaining user preferences throughout the session. It provides a way to persistently store and retrieve data specific to a user's session. Refer to: http://php.net/manual/en/reserved.variables.session.php
Which of the following data types in PHP is not scalar?
- int
- string
- array
- boolean
The non-scalar data type in PHP is an array. Arrays can hold multiple values and are not considered scalar because they are collections of values, not single values. Scalars include integers, strings, and booleans.
To check the data type of a variable in PHP, which function do you use?
is_type()
gettype()
datatype()
typeof()
To check the data type of a variable in PHP, you use the
gettype()
function. This function returns a string indicating the data type of the variable, such as "integer," "string," or "array."Which of the following is not a magic constant in PHP?
__FILE__
__DIR__
__FUNCTION__
__VARIABLE__
Magic constants in PHP are predefined constants, such as __FILE__, __DIR__, and __FUNCTION__, which provide information about the code's context. __VARIABLE__ is not a valid magic constant.
In PHP, which function is used to get the length of a string?
strlen()
count()
sizeof()
strlength()
In PHP, the
strlen()
function is used to determine the length (number of characters) of a string. It's particularly useful for validating input or working with text data.