Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "GLOBAL" and "STATIC" variable in PHP?

Tags:

php

What exactly is the difference between the GLOBAL and STATIC variables in PHP? And which one is preferable to use, when we want to use a variable in multiple functions?

Thanks.

like image 898
Chetan Sharma Avatar asked Apr 22 '12 07:04

Chetan Sharma


1 Answers

A static variable just implies that the var belongs to a class but can be referenced without having to instantiate said class. A global var lives in the global namespace and can be referenced by any function in any class. Global vars are always frowned upon because they're so easily misused, overwritten, accidentally referenced, etc. At least with static vars you need to reference via Class::var;

like image 166
AlienWebguy Avatar answered Nov 15 '22 01:11

AlienWebguy