Yes you should generally avoid using global variables because this increases the likelihood that you might end up with a "name collision" -- this is when two different sections of code try to use the same exact variable name for what should be different objects. E.g., I might write some code and use $db to represent a MySQL connection using the old mysql functions. Some other guy might use it to create a MySQL connection using the newer mysqli functions -- and you can run into really weird behavior. Aside from name collisions, there may be other reasons.
It is, however, convenient. If you have twenty five functions that all use the database, you don't want each function to have to connect/disconnect -- this would obviously not be ideal from a performance perspective and you could even have lots of redundant code. If your code project is small (and you expect it to stay small) then using a global var for $db might be easier than doing something else. On the other hand, if you expect your project to get quite large and you'll be using third-party code, you'll probably want to avoid lots of globals and you should be explicit about what database your functions are using -- either pass in $db as a param to the function or use OOP and supply the $db object as a constructor to some kind of object perhaps.