Why the functions.php of theme is loaded so many times? where should i take a look?
It doesn't, that's not how WP themes work. If you're manually loading
functions.php then that would be why, but you shouldn't do that.
functions.php and WP are laoded 6 times.
The only other thing I can think of, is that you've modified WordPress itself to get the changes you wanted, never do this. Use themes plugins actions and filters to change the behaviour of WordPress.
Which is the proper way to do the scenario I described? Run a function once when user/visitor enters the site
You would need to leave some kind of indicator that the visitor has been there before, aka a cookie.
I've separated the last part of this question into its own thing as it suggests you have a fundamental misunderstanding of how PHP programs work.
and then use either global or session variable
A PHP session is a bad idea, as it still involves a cookie on the user side, so it does not bypass cookie laws. It also won't work on a lot of hosts as they'll either turn this off, or their caching and optimisations would be incompatible.
So why not use a global variable?
Because that's not how PHP works. PHP programs aren't like a Node or a Java CMS that runs in the background. There's no persistant program.
Everytime you make a request, a PHP process is spun up, from scratch, each request is a blank slate. No variables you store in PHP will be available, and all files are loaded.
WordPress will try to mitigate this by being smart about how it loads things from the database, providing APIs for persistent storage, etc. If you have a memcached or Redis server you can install an object cache that can give a huge performance boost, but you can't just declare a variable to have a value and expect it to still have that value on the next page load. At the end of the request, the program ends, it's all wiped clean.
So what are
global variables in PHP?
global keyword is a way to make a variable available everywhere without any structure. It's a crude way of pulling something out of thin air that can cause lots of problems. It's not just a feature of PHP applications either, the scourge of global state is known in many programming languages. Look at the Google Clean Code lectures on global state to learn about the horrors and dangers.
You'll also notice some WP developers are big fans of "singletons". This is when they take things and put them in a class so it looks like it's object oriented, but it isn't. Then they give it a static method so they can access it from anywhere like a
global variable. It's a well known anti-pattern and bad practice.
So What Should You Do?
That depends on what it is that you're doing, your question is very light on details.
If you want to know if a user has visited the site before:
- set a cookie if no cookie is set
- if no cookie is set then they've never been here before
- if a cookie was set, then this isn't the first time they've visited
None of this is WordPress though, it would be the same answer for all CMS' and languages.
Note that this is not foolproof, anybody can:
- visit in incognito mode
- turn off cookies in their browser
- send a GDPR request demanding all the data you have on them, and to be removed
- use someone elses device
- use a different browser
As for making a remote API request:
- Don't do it in PHP on the frontend, this is will make your site ultra slow as it has to wait for the remote server before it can reply to the user with the page
- expect a major SEO hit as a result
- This opens up additional options for testing if the user has been to the site before, such as checking localstorage instead of using cookies
- This is significantly faster, the server will never be able to match this speed if done correctly