Php constant _ PHP Tutorial

Source: Internet
Author: User
Tags parse error
Php constants. Naming rules for constants: the same naming rules as variables: define () function format: define (constant name, value); example: define (PI, 3.14 ); constants and variables are the same: 1: naming rules for naming constants

Naming: The same naming rules as variables

Method: define () function

Format: define ('constant name', 'Specific Value ');

Example: define ('pi ', 3.14 );

Comparison between constants and variables

Same:

1: The naming rules are the same (but generally in uppercase)

Different:

1: $ is not used when a constant is referenced, and the name is used directly.

2: constants can only be defined using define, but cannot be defined using a value assignment statement.

3: once defined, constants cannot be redefined or undefined.

4: the constant value can only be a scalar (only integer, floating point, Boolean, numeric, or NULL)

[Php]

// Define a constant

Define ('pi ', 3.14 );

// Area of the computing Park

$ R = 3;

Echo pi * $ r;

// Result: 28.26

// After a constant is defined, it cannot be redefined.

Define ('pi ', 3.23 );

// Result: Notice: Constant pi already defined in C: \ wamp \ www \ 0124 \ 05.php on line 15

// The constant cannot be assigned a value again.

Pi = 100;

Echo pi;

// Result: Parse error: syntax error, unexpected '= 'in C: \ wamp \ www \ 0124 \ 05.php on line 21

// Syntax errors

$ PI = 10;

Function text (){

Echo $ PI; // The function has a scope. This $ PI variable is not the same as $ PI in the function.

Echo pi; // A constant defined once, which can be used anywhere

}

// Result: Notice: Undefined variable: PI in C: \ wamp \ www \ 0124 \ 05.php on line 31

Text ();

?>

How can I determine whether a constant is defined?

Define defines constants

Defined judge constant

[Php]

If (defined ('pi ')){

Echo 'pi constant has been defined.
';

} Else {

Echo 'pi constant is undefined. let me define it.
';

Define ('pi ', 33322 );

}

Echo PI;

?>

[Php]

Result: The PI constant is undefined. I will define it.

33322

Naming rule: the same naming rule method as the variable: define () function format: define (constant name, specific value); example: define (PI, 3.14 ); constants and variables are the same: 1: name...

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.