I want to use rand() php function in header.phml but if Magento cache is enabled then rand() function display old random value, but if the cache is disabled then it is working fine. so please give me the suggestion.


1 Answer 1


Try to override that block file and add cachable = "false" in your layout file it will work. For example check below, this will disable the cache for that particular part even the cache enabled in backend.

<referenceContainer name="header.panel">
        <block class="Vendor\Module\Block\Currency" name="currency" template="Vendor_Module::currency.phtml" cacheable="false"/>
  • Thank you :) It's working fine in my case. Nov 17, 2017 at 10:36
  • 4
    @jeeva make cachable = "false" is making whole page not particular block & you are adding in header so whole site is not cacheable
    – Yogesh
    Nov 17, 2017 at 10:37
  • Only header block is not cacheable right? We are adding that for particular block how the whole site won't work..If any thing i said wrong means please tell me your suggestion Nov 17, 2017 at 10:42
  • 3
    Do not use this! As Yogesh said you will lose caching for every page on your site!
    – Ben Crook
    Nov 17, 2017 at 12:47
  • Thank you for all suggestions. Can you give me proper way for this because I new in magento 2. Nov 20, 2017 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.