Difference between overall, const and define()

Asked

Viewed 111 times

6

So I was studying about POO and MVC structures with PHP and saw that we often need to use global variables (accessible throughout the application) and what’s the difference of using:

global $nome = 'Joao';
define(NOME,'Joao');
const NOME = 'Joao';

1 answer

5


global declares a normal variable with global life span and time (is not only where it was declared), therefore it exists for the entire time of the execution from the time of its statement. This in itself already shows how problematic it can be, because somewhere you can access something that has not been declared. There are others problems related to global state. In the way people use PHP they should avoid as much as possible. If they use script which would be the right thing to do in a language of script then variable with this scope would not be so problematic as it has little to observe and can maintain more control.

The rest of the question can be checked in question already asked before because it has nothing to do with variables: What is the difference between define() and const?, both do not vary and are global.

  • Thank you very much friend, it helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.