title: PHP - GETH - Cannot return variable inside function
body: <p>So I am calling a function to get the balance of an ethereum account.</p>
  <p>I am using the php web 3 found <a href="https://github.com/web3p/web3.php/" rel="nofollow noreferrer">here</a>.</p>
  <p>I wrapped the web3 class inside my own function.</p>
  <p>So I call my function -&gt; my function executes the web3 eth command.</p>
  <p><em><strong>My function that calls the eth command:</strong></em></p>
  <pre><code>    public function getAccountBalance($account) {
  
          $newBalance = '';
  
          $this-&gt;web3-&gt;eth-&gt;getBalance($account, function ($err, $balance) use($newBalance) {
  
              if ($err !== null) {
                  echo 'Error: ' . $err-&gt;getMessage();
                  return;
              }
  
              $newBalance = $balance-&gt;toString();
              echo $newBalance; // this echos the balance fine
  
          });
          echo $newBalance; // this returns empty, as like we defined at the top
          return $newBalance;
      }
  </code></pre>
  <p>I am trying to return the balance from the eth function to return it within my getAccountBalance() function but whenever I try to return it, it gives empty, as if it didn't update the $newBalance value inside the <code>$this-&gt;web3-&gt;eth-&gt;getBalance($account, function ($err, $balance) use($newBalance) {</code> callback.</p>
  <p>If I echo it inside the <code>$this-&gt;web3-&gt;eth-&gt;getBalance($account, function ($err, $balance) use($newBalance) {</code> callback, it outputs the correct balance fine.</p>
  <p>If I try and echo it outside of the <code>$this-&gt;web3-&gt;eth-&gt;getBalance($account, function ($err, $balance) use($newBalance) {</code>, it gives me the value at the top of my function, where I define it: <code>$newBalance = '';</code>, so it's giving me an empty response.</p>
  <p>I am not sure why this command does not let me and some others do...</p>
  <p>I have tried adding global $newBalance instead of use($newBalance) too but still no luck.</p>