Answers.Body: <p>From your example, the <code>Global</code> handle is <code>globalWebsite</code> and the field handle is <code>freeTrialPeriodDays</code>.</p>
  <p>So this should work in PHP:</p>
  <pre><code>$trialDays = \Craft::$app-&gt;getGlobals()-&gt;getSetByHandle('globalWebsite')-&gt;freeTrialPeriodDays;
  </code></pre>
  <p>You should probably do this in two steps so you can check that a <code>Global</code> has been returned before you try to query the field value on it:</p>
  <pre><code>$myGlobal = \Craft::$app-&gt;getGlobals()-&gt;getSetByHandle('globalWebsite');
  if ($myGlobal) {
      $trialDays = $myGlobal-&gt;freeTrialPeriodDays;
  } else {
      $trialDays = 'some default value';
  }
  </code></pre>
  <p>This can be simplified using the null coalescing operator like this:</p>
  <pre><code>$trialDays = \Craft::$app-&gt;getGlobals()-&gt;getSetByHandle('globalWebsite')-&gt;freeTrialPeriodDays ?? 'some default value';
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-10T18:02:14.610Z
Answers.Text: 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.
Answers.UserDisplayName: Sylvain Tremblay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-10T17:27:43.850Z
Answers.LastActivityDate: 2022-01-10T17:27:43.850Z
Answers.UserDisplayName: Steve Rowling
Body: <p>In twig, I can get the value this way,
  <code>globalWebsite.freeTrialPeriodDays</code></p>
  <p>I need to get this value in PHP, but cleary this won't work;</p>
  <p><code>$trialDays = \Craft::$app-&gt;getGlobals()-&gt;getSetByHandle('freeTrialPeriodDays');</code></p>
  <p>I know it's simple, can't get the right way.</p>
  
Tags: <globals>
Title: Get global variable value PHP
ThreadText: # Get global variable value PHP
  
  (Asked by: username_0 on 2022-01-10)
  
  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.
  
  **Tags:** globals
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-10. Score: 4 | Accepted answer
  
  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';
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  