PHP Framework-What is the difference between define vs Const in PHP definition of a constant?

Source: Internet
Author: User
Tags php definition
What is the difference between a variable and one?

define('SOMETHING', true);andconst SOMETHING = true;

Performance or otherwise?

Reply content:

What is the difference between a variable and one?

define('SOMETHING', true);andconst SOMETHING = true;

Performance or otherwise?

Baidu can solve the problem, the landlord did not self-study spirit?

【问】在php中定义常量时,const与define的区别? 【答】使用const使得代码简单易读,const本身就是一个语言结构,而define是一个函数。另外const在编译时要比define快很多。(1).const用于类成员变量的定义,一经定义,不可修改。define不可用于类成员变量的定义,可用于全局常量。(2).const可在类中使用,define不能。(3).const不能在条件语句中定义常量。例如:    if (...){        const FOO = 'BAR';    // 无效的invalid    }     if (...) {        define('FOO', 'BAR'); // 有效的valid    }(4).const采用一个普通的常量名称,define可以采用表达式作为名称。    const  FOO = 'BAR';     for ($i = 0; $i < 32; ++$i) {        define('BIT_' . $i, 1 << $i);    }(5).const只能接受静态的标量,而define可以采用任何表达式。例如:    const BIT_5 = 1 << 5;    // 无效的invalid     define('BIT_5', 1 << 5); // 有效的valid(6).const定义的常量时大小写敏感的,而define可通过第三个参数(为true表示大小写不敏感)来指定大小写是否敏感。例如:    define('FOO', 'BAR', true);     echo FOO; // BAR    echo foo; // BAR

From: Php Constants explained: The difference between define and const

  • 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.