69.5k views
4 votes
Which PHP command is used to retrieve a cookie value?

A) $_COOKIE['cookie_name']
B) getCookie('cookie_name')
C) fetchCookieValue('cookie_name')
D) requestCookie('cookie_name')

User Ben Alan
by
7.5k points

1 Answer

4 votes

Final answer:

The PHP command for retrieving a cookie value is $_COOKIE['cookie_name'], accessing it through the $_COOKIE superglobal array. It's important to check if the cookie is set using isset() to avoid errors.

Step-by-step explanation:

The PHP command used to retrieve a cookie value is A) $_COOKIE['cookie_name'].

In PHP, cookies are stored in the $_COOKIE superglobal array, which is an associative array that contains all the cookies that are accessible by the script currently executing. To retrieve the value of a cookie, you simply access the $_COOKIE array with the name of the cookie as the key. It's important to remember to check if the cookie is set before trying to use it to avoid undefined index notices.

Example:

<?php
if(isset($_COOKIE['cookie_name'])) {
echo 'Value of the cookie: ' . $_COOKIE['cookie_name'];
} else {
echo 'Cookie does not exist.';
}
?>
User Kishore
by
8.3k points