What is the difference between static and constant in php?

What is a variable ?

A variable, its value can be changed by the program at runtime. The accessibility or the scope of a variable refers to where the variable can be read from or written to, and its lifetime, or how long it stays in the computer memory.

e.g.

Constant

A Constant is something that will always remain the same though out the entire lifetime of a program. A Constant variable cannot be modified after it defines and it cannot be change throughout the program. The Constant with a fixed value tells the compiler to prevent the programmer from modifying it. Whenever you try to change it, it will throw an error message. Constant variables declares with const keyword and can be used with primitive data types . Constants are set at compile time itself and assigned for value types only.

e.g.

Static

Static variable is a property of a Class rather than the instance of class. It is stored on the data segment area of memory and the same value is get shared to all instances of that class. It can be assigned for reference types and set at run time. The static modifier can be applied with classes, fields, methods, properties, operators, events and constructors. It represent a kind of a global value for all the instances of that class and can able to call them using class name.

Where Product is class name and price is a static declaration.



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. By default, constant identifiers are always uppercase. Usually, a Constant name starts with an underscore or a letter which is followed by a number of letters and numbers. They are no need to write a constant with the $ sign. The constant() function is used to return the value of a constant.

    Example:

    PHP

    <?php

       define("Hello", "Welcome to geeksforgeeks.com!");

       echo Hello;

    ?>

    Output:

    Welcome to geeksforgeeks.com!

    PHP Variables:A PHP variable is a name given to a memory address that holds data. The basic method to declare a PHP variable is by using a $ sign which is followed by the variable name. A variable helps the PHP code to store information in the middle of the program. If we use a variable before it is assigned then it already has a default value stored in it. Some of the data types that we can use to construct a variable are integers, doubles, and boolean.

    Example:

    PHP

    <?php

       $txt = "Hello from geeksforgeeks!";

       $x = 5;

       $y = 10.5;

       echo $txt;

       echo "<br>";

       echo $x;

       echo "<br>";

       echo $y;

    ?>

    Output:

    Hello from geeksforgeeks!
    5
    10.5

    Difference between PHP Constants and PHP Variables

    PHP Constants PHP Variables
    In PHP constants there is no need to use $ sign. In PHP Variables the $ sign is been used.

    The data type of PHP constant cannot be changed during the

    execution of the script.

    The data type of the PHP variable can be changed during the

    execution of the script.

    A PHP constant once defined cannot be redefined. A PHP variable can be undefined as well as can be redefined.

    We can not define a constant using any simple assignment operator

    rather it can only be defined using define().

    We can define a variable using a simple assignment operation(=).
    Usually, constants are written in numbers. On the other hand, variables are written in letters and symbols.
    PHP constants are automatically global across the entire script. PHP variables are not automatically global in the entire script.
    PHP constant is comparatively slower than PHP variable A PHP variable is comparatively faster than the PHP constant

    What is the difference between static and constant?

    It cannot access non-static data members not even call non-static member functions. It can be called even if no objects of the class exist. ... C++.

    What is the difference between static const and const?

    const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that . c file and cannot be seen outside of it.

    What is difference between constant and variable in PHP?

    A PHP constant once defined cannot be redefined. A PHP variable can be undefined as well as can be redefined. rather it can only be defined using define(). We can define a variable using a simple assignment operation(=).

    What is the difference between static and final in PHP?

    final static declares a method which is static (can be called without an instance of the class) and final (can't be overridden by subclasses). static alone can be used to define a class-scoped variable, which isn't constant (but variables can't be final ).