Knowee
Questions
Features
Study Tools

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,SCOOKIE,_SESSION, S_COOKIE,_REQUEST with example

🧐 Not the exact question you are looking for?Go ask a question

Solution

In PHP, there are three types of arrays:

  1. Indexed arrays: An array with a numeric index where values are stored linearly.
  2. 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.
  3. Multidimensional arrays: These are arrays that contain other arrays within them.

Now, let's discuss SESSION,_SESSION, _COOKIE, and $_REQUEST.

  1. $_SESSION: This is a superglobal variable which is used to store information in a variable to be used across multiple pages. A session is started with the session_start() function. For example:
<?php
   session_start();
   $_SESSION["name"] = "StudyAssistant";
   echo 'Hello, ' . $_SESSION["name"];
?>

In this example, a session is started and a name is stored in the session variable. This name can now be accessed on any page that starts with session_start().

  1. $_COOKIE: This is a superglobal variable which is used to store information on the user's computer. For example:
<?php
   setcookie("name", "StudyAssistant", time() + (86400 * 30), "/"); // 86400 = 1 day
   if(!isset($_COOKIE["name"])) {
     echo "Cookie named 'name' is not set!";
   } else {
     echo "Cookie 'name' is set!<br>";
     echo "Value is: " . $_COOKIE["name"];
   }
?>

In this example, a cookie named "name" is set to "StudyAssistant". The cookie is set to expire in one day.

  1. $_REQUEST: This is a superglobal variable which is used to collect data after submitting an HTML form. For example:
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="name">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_REQUEST['name'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>

</body>
</html>

In this example, when the user inputs their name and submits the form, the name is collected and displayed on the page.

This problem has been solved

Similar Questions

How many types of arrays are there in PHP?Group of answer choices5231

Which of the following is an associative array of variables passed to the current script via HTTP cookies?A.$GLOBALSB.$_SERVERC.$_COOKIED.$_SESSION

Which of the following is an associative array of variables passed to the current script via HTTP cookies?

Which option best describes the segment of code below?  $new_user = array(        "firstname" => $_POST['firstname'],        "lastname"  => $_POST['lastname'],        "email"     => $_POST['email'],        "age"       => $_POST['age'],        "location"  => $_POST['location']    );Question 15Answera.$new_user is an array that stores data (from the session super global array) as values.b.None of these.c.$new_user is an associative array that stores form data as keys.d.$new_user is a multidimensional array that stores form data as keys.e.$new_user is a multidimensional array that stores form data as values.f.$new_user is an associative array that stores form data as values.

Learn about arrays, functions, and classes in PHP.

1/2

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.