hi everyone,

keep getting this error although it shows the result perfectly..i cant do a $cart= ''; as it will show the incorrect result


Notice: Undefined variable: cart in D:\xampp\htdocs\book\includes\functions.inc.php on line 8

<?php
function writeShoppingCart() {


if(isset($_SESSION['cart']) ) {
	$cart = $_SESSION['cart'];
	}
	if (!$cart) {
		return '<p>You have no items in your shopping cart</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		$s = (count($items) > 1) ? 's':'';
		return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
	}

If the session is not available, cart has no value.
Instead of:

if (isset($_SESSION['cart'])) {
  $cart = $_SESSION['cart'];
}

Use:

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : false;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.