What is the difference between define() and const in PHP?

Please explain me the main difference between define() and const.

Comments

  • This explains well.

  • JohnYXJohnYX Cebu
    edited November 2014

    const could not be used in the global scope. You could only use this from within a class. This should be used when you want to set some kind of constant option or setting that pertains to that class. Or maybe you want to create some kind of enum.

    define can be used for the same purpose, but it can only be used in the global scope. It should only be used for global settings that affect the entire application.

  • A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script ..it could not be used in the global scope. You could only use this from within a class. This should be used when you want to set some kind of constant option or setting that pertains to that class. Or maybe you want to create some kind of enum.http://cedarsoftware.co.uk/webapplications/

    define can be used for the same purpose, but it can only be used in the global scope. It should only be used for global settings that affect the entire application.

  • A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script ..it could not be used in the global scope. You could only use this from within a class. This should be used when you want to set some kind of constant option or setting that pertains to that class. Or maybe you want to create some kind of enum.( http://cedarsolutions.in )

    define can be used for the same purpose, but it can only be used in the global scope. It should only be used for global settings that affect the entire application.

  • Const defines constants at compile time and is a variable that doesn't change in value. Since define() is an immediate value, which defines them at run time. You can assert a pointer to a const, but can not to a define(), even though a define() can be a pointer.
    consts are always case sensitive, but define() allows you to define case insensitive. Unless you need any type of conditional definition, you can use const instead of define(), simply for the sake of readability.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories