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

Howdy, Stranger!

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

Categories

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

Damian99Damian99 USAPosts: 116Member

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

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member

    This explains well.

  • JohnYXJohnYX CebuPosts: 3Member
    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.

  • william stevewilliam steve kakkanadPosts: 3Member

    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.

  • william stevewilliam steve kakkanadPosts: 3Member

    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.

  • AgriyaAgriya IndiaPosts: 2Member

    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.