Getting Started with PHP (2)

Source: Internet
Author: User
Tags getting started with php

What is a constant

What is a constant? Constants can be understood as a constant amount of value (such as pi), or when a constant value is defined, it cannot be changed anywhere else in the script. Constants in PHP are divided into custom constants and system constants (which are described in detail in subsequent subsections).

A custom constant is a constant defined according to the needs of our development, and it is defined by using the function define () in PHP. (Note: function, we can be understood as a concrete mixer, or a sieve, through the entrance to provide raw materials, and then export output results, in the function also allows the entrance does not provide any data, export also allows to return no value. )

The syntax format for the define () function is:

BOOL Define (String $constant _name, mixed $value [, $case _sensitive = True])

It has 3 parameters (i.e. raw material):

The first parameter, "Constant_name", is a required parameter, a constant name, or a marker, and the name of the constant is consistent with the variable, but be aware that it does not have a dollar sign. The second parameter, "value," is a required parameter, which is the value of the constant. The third parameter, "Case_sensitive", is an optional parameter, specifying whether it is case sensitive, set to true to be insensitive, and generally without specifying a third parameter, the value of the default third parameter is False.

(Note: string indicates that the parameter type is a string type, mixed indicates that the parameter type can be accepted as a number of different types, Case_sensitive = True indicates that the default is Boolean type True)

Meet System constants

System constants are constants that PHP has already defined, and we can use them directly, and common system constants are:

(1) __file__:p hp program file name. It can help us get the current file in the physical location of the server.

(2) __line__:P HP program file line number. It can tell us the current code in the first few lines.

(3) Php_version: The version number of the current parser. It can tell us the current PHP parser version number, we can know in advance whether our PHP code can be parsed by the PHP parser.

(4) Php_os: Executes the current PHP version of the operating system name. It can tell us the name of the operating system used by the server, and we can optimize our code based on the operating system.

Constants are defined, then constants are used, so how do we get constant values?

There are two ways to take a value for a constant value. The first is to get the value directly using the constant name, for example to calculate the area of Pi, as follows ("*" is an operator, equivalent to multiplication sign, which is described in the following sections):

PI* $r * $R; Calculate the area of a circle?>

The second is the use of the constant () function. It is the same as the direct use of the output of the constant name, but the function can be dynamically output different constants, in the use of flexible, convenient, the syntax format is as follows:

Mixed constant (string constant_name)

The first parameter, Constant_name, is the name of the constant to get, or a variable that stores the constant name. If successful, the value of the constant is returned, and failure indicates that the error message constant is not defined. (Note: Mixed indicates that the return value type of the function is of many different types, string indicates that the parameter type is a string type)

For example:

Getting Started with PHP (2)

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.