PHP Learning Notes _4_ constants and magic constants points to note

Source: Internet
Author: User
In PHP, variables correspond to constants, constants are system constants, frame constants, and custom constants.
Defining constants can use the following


    
         define("MY_FIELD",123);    echo MY_FIELD;// 输出123// 如果定义常量是一个变量$name = "TEST";    define($name,"456");    echo$name;// 输出TESTecho constant($name);//输出真正的变量值456?>

Note the point:
1. Use directly, without adding ' $ ' sign;
2. Constants can be defined and used anywhere;
3. You can use the constant name or the constant (constant name) function to get the value of the constant;
4.get_defined_constants (); Can get all the constants that have been defined;

Magic constants: added by different extension libraries, the usual magic constants are the following:

name Description
__LINE__ The current line number in the file.
__DIR__ The directory where the current file resides.
__FILE__ The full path and file name of the file. If used in the include file, the include filename is returned. Since PHP 4.0.2, FILE always contains an absolute path, and the previous version sometimes contains a relative path.
__FUNCTION__ The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.
__CLASS__ The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.
__METHOD__ The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).
__NAMESPACE__ The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)

Copyright NOTICE: This article is the original article, reprint need to indicate the source, the article view only represents the view at that time, there must be insufficient, welcome to make bricks remind, thank you very much!

The above describes the PHP learning notes _4_ constants and magic constants need to pay attention to the points, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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