The difference between const and define () in PHP, select

Source: Internet
Author: User

From: Http://stackoverflow.com/questions/2447791/define-vs-const

Same point: Both can define constants

Const  foo = ' Bar ';d efind (' foo ', ' Bar ');

The disadvantage of const:

1.const must be declared in Top-level-scope (top-level domain):

For example:

if (condition) {    const FOO = ' BAR ';   // Not defined           }// but if(condition) {    defind (/// definition          }

A common detection constant is defined in the way:

if (! defined (' FOO ')) {    define(' FOO ', ' BAR ');}

2.Const accepts a static scalar type (number,string,true.false,null,__file__,......), whereas Defind () accepts any expression.

However, constant expressions since PHP 5.6 are also allowed to be used in const:

Const Bit_5 = 1 << 5;    // PHP 5.6 After valid, before invalid Define // Always valid

3.Const accepts a plain (? Do not know what to translate, is constant name fixed meaning) constant name, however, Defind () accepts any expression as a constant name

 for ($i$i <, + +$i) {    define$i$i);}

4.const-defined constants names are case-sensitive, define () allows you to be case insensitive, as long as the third argument is passed true:

Define (' FOO ', ' BAR ',true);
Echo FOO; BAR
echo foo; BAR
Echo Foo; BAR

This is the bad side of the const, so let's look at the reason why I personally recommend the use of const unless this happens .

1.Const readable, is a language structure, not a method, or you can use a definition constant in a class

2.const can define constants in the current namespace, but define () has to pass the full namespace name. (one can be relative, one must be absolute)

namespace a\b\c; // To define the constant A\b\c\foo: Const FOO = ' BAR '; Define (' A\b\c\foo ', ' BAR ');

3.Const Since PHP 5.6 defines a constant value can use an array, however define () does not support arrays, but in PHP 7 it will support this feature.

Const FOO = [1, 2, 3];    // valid in PHP 5.6 Define // Invalid in PHP 5.6, valid in PHP 7.0

4.Const is a language structure, defined at compile time, define () defined at run time, const is faster than Defind ()

5. Finally, const can define constants in the class, and define () cannot.

class Foo {    const//  valid}//  butclass  Baz {    define//  Invalid}

Summary

Unless you want to use an expression or define a constant in a conditional judgment statement, using const is a necessary medicine for your home travel, kill people kill.

The difference between const and define () in PHP, select

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.