Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How PHP Globals Work

Olivia Rhye

I had a reminder of how globals work recently. I was trying to perform a WordPress query using the $wpdb global for the database. The global was declared at the top of the file like this:

global $wpdb;

Simple enough. Then I thought I would just use it inside of another function, since it's global. Functions, however, don't recognize the global when it is used, unless you tell the function that it is supposed to use the global, rather than a new function. So you will need to declare it again within the function.

global $wpdb;

Then you can use it within the function just like any other variable.

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy