Game leveling system
First: in the book 'Refactoring' by Martin Fowler,  one of the code smells is "Obsession with elementary types". And the advice is to create/take out variables and move them to another class. 

Array is an elementary type. The resolution is to create an entity class Settings and set member variables $lvl,$xp,$nxtlevel.

Second: global $db; transfer through object.

Using global variables is bad practice. If you want to use a Database you should create an object for Database access. Best practice use DI. For PHP one such library is php-di.

Your code may look like this:

&lt;?php

  class LevelingSystem {

  private $settings;
  private $db;

  function __construct(SettingsEntity $settings, DBInterface $db ){
    $this-&gt;settings=$settings;
    $this-&gt;db = $db;
  }

  public function setxp($setxp) {
    $result =  $this
                -&gt;db
                -&gt;prepare("UPDATE user_atri SET totalxp =totalxp+" . $setxp . " WHERE id=1")
                -&gt;execute();

    if(!$result) {
       throw new Exception('DB error');  //you can send your exception 
    }        

    return $this;
  }

  public function calcSettings() {
   $xp = $this-&gt;getxp();
   $nxtlevel = $this-&gt;settings-&gt;getNxtlevel();
   $lvl = $this-&gt;settings-&gt;getLvl();

   while ($xp &gt;= $nxtlevel) {
        $lvl += 1;
        $xp = $xp - $nxtlevel;
        $nxtlevel = round($nxtlevel * 1.6);
    }

     $this-&gt;settings
     -&gt;setlvl($lvl)
     -&gt;setXp($xp)
     -&gt;setNxtlevel($nxtlevel);

     return $this;
  }

  public function getLvl() {
      return $this-&gt;settings-&gt;getLvl();
  }

  private function getxp() {
    $totalxp = $db
                -&gt;prepare("SELECT totalxp FROM user_atri where id=1")
                -&gt;execute()
                -&gt;fetch(PDO::FETCH_ASSOC);

    if(!isset($totalxp["totalxp"])) {
       throw new Exception('Some DB error'); //you can send your exception 
    } 

    return $totalxp["totalxp"];
  }
}