The constant used to learn php at the front end and the constant used to learn php

Source: Internet
Author: User
Tags php define

The constant used to learn php at the front end and the constant used to learn php
* Directory [1] definition constant [2] constant detection [3] system constant [4] before magic constant

Constants do not exist in javascript. in php, they are important content that is tied with variables. Constants are similar to variables, but once defined, constants cannot be changed or undefined. Constants are mainly used to avoid repeated definitions, tamper with variable values, and improve code maintainability. The following describes the variables in php.


Define Constants

A constant is an identifier of a simple value. As its name implies, once a constant is defined during script execution, the definition cannot be changed or canceled. Constants are case sensitive by default. By convention, constant identifiers are always capitalized.

Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underscore followed by any letter, number, or underline.

Like a global variable, the constant range is global. You can access constants anywhere in the script without worrying about the Zone.

Constants can only contain scalar data (boolean, integer, float, and string ). Resource constants can be defined, but should be avoided as far as possible, because unexpected results may occur.

Define () function

Define constants use the define () function, which uses three parameters: the first parameter defines the constant name, and the second parameter defines the constant value, the third parameter specifies whether the constant name is case sensitive. The default value is false.

bool define ( string name, mixed value [, bool case_insensitive] )
<? Php $ p = 'pi0'; define ('Pi ', 3.14); define ('Pi', 3.15); // invalid, because constants cannot be modified to define ($ p, 3.14); echo PI; // 3.14 echo "<br>"; echo PI0; // 3.14?>


After PHP5.3.0, you can use the const keyword to define constants outside the class definition.

The use of the const keyword to define a constant must be at the top of the function region, because this method is defined during compilation. This means that you cannot use const to define constants in the function, in the loop, or in the if statement.

<? Php // the following code works properly after PHP 5.3.0 const CONSTANT = 'Hello world'; echo CONSTANT ;?>


Constant Detection

For constants, it is usually necessary to check whether a constant is defined or the value of a constant. It involves the defined () function and the constant () function.

Defined () function

The defined () function is used to determine whether a constant has been defined. Its Syntax format is:

bool defined(string constants_name)

If yes, true is returned. Otherwise, false is returned.

If the Constant is repeatedly defined, the PHP parser will issue a warning of "Constant XXX already defined", reminding that the Constant has been defined

<?php define("PI1",3.14);$p = "PI1";$is1 = defined($p);$is2 = defined("PI2");var_dump($is1);//bool(true)var_dump($is2);//bool(false)?>

Constant () function

The constant () function is used to return the value of a constant. The syntax format is:

mixed constant(string constant_name)
<? Php $ p; define ("PI1", 3.14); define ("PI2", 3.142); $ height = "medium"; if ($ height = "medium ") {$ p = "PI1" ;}else {$ p = "PI2" ;}$ r = 1; $ area = constant ("PI") * $ r; echo $ area;?>


System constant

In php, in addition to defining constants, a series of system constants are predefined and can be directly used in programs to complete some special functions. Below are some predefined constants that are common in the system.

The current PHP version number E_ERROR 1, such as the operating system name PHP_ OS for PHP parsing, such as UNIX or WINNT, is incorrect. This causes the PHP script to stop running E_WARNING 2, it will not cause the PHP script to stop running E_PARSE 4 parsing error. The CAP parser reports E_NOTICE 8 non-critical errors. For example, the variable does not initialize the PI value in M_PI 3.1415926535898 mathematics.

Complete System Constant list now

echo PHP_VERSION;//5.5.12echo "<br />";echo PHP_OS;//WINNTecho "<br />";?>


Magic constant

In PHP, eight system constants are changed based on their locations. Such constants are called magic constants.

The current row number in the _ LINE _ file. The complete path and FILE name of the _ FILE. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a directory with the relative path _ DIR. If it is used in included files, the Directory of the included files is returned. Unless it is the root directory, the name of the directory does not include the slash at the end. (New in PHP 5.3.0) _ FUNCTION name (New in PHP 4.3.0 ). Starting from PHP5, this constant returns the name (case sensitive) when the function is defined ). In PHP4, this value is always a lowercase letter. _ CLASS name (New in PHP 4.3.0 ). Starting from PHP5, this constant returns the name (case sensitive) when the class is defined ). In PHP4, this value is always a lowercase letter. The class name includes the declared region (for example, Foo \ Bar) _ TRAIT _ Trait name (New in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the METHOD name of the declared function area (for example, Foo \ Bar) _ METHOD _ class (New PHP 5.0.0 ). Returns the name (case sensitive) _ NAMESPACE _ name of the current NAMESPACE when the method is defined (case sensitive ). This constant is defined during compilation (New in PHP 5.3.0)
<?phpecho __FILE__;//D:\wamp\www\1.phpecho "<br />";echo __LINE__;//11?>

Related Article

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