In twig, I can get the value this way, globalWebsite.freeTrialPeriodDays

I need to get this value in PHP, but cleary this won't work;

$trialDays = \Craft::$app->getGlobals()->getSetByHandle('freeTrialPeriodDays');

I know it's simple, can't get the right way.

1 Answer 1


From your example, the Global handle is globalWebsite and the field handle is freeTrialPeriodDays.

So this should work in PHP:

$trialDays = \Craft::$app->getGlobals()->getSetByHandle('globalWebsite')->freeTrialPeriodDays;

You should probably do this in two steps so you can check that a Global has been returned before you try to query the field value on it:

$myGlobal = \Craft::$app->getGlobals()->getSetByHandle('globalWebsite');
if ($myGlobal) {
    $trialDays = $myGlobal->freeTrialPeriodDays;
} else {
    $trialDays = 'some default value';

This can be simplified using the null coalescing operator like this:

$trialDays = \Craft::$app->getGlobals()->getSetByHandle('globalWebsite')->freeTrialPeriodDays ?? 'some default value';
  • My first reflex would really be "Duh!" but instead I will simply say THANK YOU lol Yeah, I knew it was going to look simple. Thanks again. Jan 10 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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