Cookies are small pieces of data that are stored on the client’s computer by the web browser. They are commonly used to store user preferences and other information that needs to be remembered between different sessions.

In PHP, cookies can be set using the `setcookie()` function. This function takes several parameters: the name of the cookie, the value to be stored, an optional expiration time, an optional path, an optional domain, and an optional secure flag.

Here’s an example of setting a cookie with PHP:

“`php
setcookie(‘username’, ‘john’, time() + (86400 * 30), ‘/’);
“`

In this example, we are setting a cookie named ‘username’ with the value ‘john’. The cookie will expire after 30 days (86400 seconds * 30), and the ‘/’ path means the cookie is available for the entire domain.

To retrieve the value of a cookie, you can use the `$_COOKIE` superglobal variable. Here’s an example:

“`php
$username = $_COOKIE[‘username’];
echo “Welcome back, $username!”;
“`

In this example, we retrieve the value of the ‘username’ cookie and store it in a variable called `$username`. We then use the value in an echo statement to welcome the user back.

To delete a cookie, you can use the `setcookie()` function again with an expiration time in the past. This will overwrite the previous cookie and make it invalid. Here’s an example:

“`php
setcookie(‘username’, ”, time() – 3600, ‘/’);
“`

In this example, we are setting the ‘username’ cookie to an empty string with an expiration time in the past (3600 seconds ago). The ‘/’ path means the cookie is available for the entire domain.