i was working on a ecommerce project based on magento 2.In this project i had a requirement where i need cart count in my header.phtml file.So i simply write below code

echo Mage::helper(‘checkout/cart’)->getCart()->getItemsCount();

but after using this i got a php error. So i searched internet and found a simple and short solution in 1 line code that is below

$counter = $this->helper('\Magento\Checkout\Helper\Cart'); 
    echo $counter->getItemsCount();

Magento 2 provides 2 ways to show items count. One shows the number of individual items in the cart, while the other one shows the total count of items in the cart.

Let us say that cart helper is:

$counter = $this->helper('\Magento\Checkout\Helper\Cart');

When you do:

echo $counter->getItemsCount();

it will show the number of individual items in the cart.If you want to show total items count, then use:

echo $counter->getSummaryCount();

hope this will help you too.