What are the different types of arrays in PHP? Explain $_SESSION, S_COOKIE,$_REQUEST with example
Question
What are the different types of arrays in PHP? Explain $_SESSION
, $_COOKIE
, $_REQUEST
with example
Solution
In PHP, there are three types of arrays:
- Indexed arrays: An array with a numeric index where values are stored linearly.
- Associative arrays: An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
- Multidimensional arrays: These are arrays that contain other arrays within them.
Now, let's discuss _COOKIE, and $_REQUEST.
- $_SESSION: This is a superglobal variable which is used to store information in a session variable. It's an associative array containing session variables available to the current script. Here is an example:
<?php
// Starting session
session_start();
// Storing session data
$_SESSION["name"] = "StudyAssistant";
?>
In the above example, a session variable "name" is set.
- $_COOKIE: This is a superglobal variable which is used to retrieve cookie values. It's an associative array of variables passed to the current script via HTTP Cookies. Here is an example:
<?php
// Setting a cookie
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
In the above example, a cookie named "test_cookie" is set and then a check is performed to see if cookies are enabled.
- _GET, _COOKIE. Here is an example:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
In the above example, the form data is sent with the HTTP POST method. When the user clicks the submit button in the HTML form, the form data is sent to "php_self". The "php_self" is a super global variable that returns the filename of the script that's being executed. The request method is checked and if it's a POST method, the form data is handled.
Similar Questions
Which of the following is an associative array of variables passed to the current script via HTTP cookies?A.$GLOBALSB.$_SERVERC.$_COOKIED.$_SESSION
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each
What are differences between Array of Objects vs. Array Variables? Hints: Concept, Use Case Syntax and example
Which of the following function is used to set cookie in PHP?Group of answer choicesmakecookie()createcookie()None of the abovesetcookie()
Which of the following is a middleware that parses cookies attached to the client request object?(1 Point)cookie-parsercookiesNone of the abovecookie
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.