logo

:: What is the difference between static variable and a constant? ::

Question:

What is the difference between static variable and a constant?


Answer:

You can change the value of a static variable, you can't with constants.

class Foo {
   public static $counter = 1;
   const SomeConstant = 10;
}

Foo::$counter++;
echo Foo::$counter; // This should give us '2'
echo Foo::SomeConstant; // This should give us '10'
Foo::SomeConstant++; // Error. Cannot change the value of a constant.

<< Back to Questions and Answers

© Copyright php-etc.com 2010. Please contact the webmaster for all enquiries.