How to define and use constant _php techniques in PHP

Source: Internet
Author: User
Tags constant php class

1. Custom Constants

* Must be defined with function define ()
* The value can not be changed after the definition is finished
* Use the constant name directly, not as a variable in front plus $s
For example: Define ("PI", 3.14); Define a constant
$area = Pi*r*r; Calculate the area of a circle
Define ("URL", "http://www.jb51.net");
echo "My URL is:". URL;

2 System constants:

File:P hp program filename
Line:P number of HP program files
Php_version: Version number of the current parser
Php_os: Run the current PHP version of the operating system name
You can use it directly, for example, to see the name of the operating system that executes the current PHP version, you can write the echo php_os

PHP defines and uses a class constant

PHP class Constants

We can define constants in the class. The value of the constant will always remain unchanged. You do not need to use the $ symbol when defining and using constants.

The value of a constant must be a fixed value and cannot be the result of a variable, class property, or other operation, such as a function call.

Its also possible for interfaces to have constants. Look in the interface documentation for examples. You can also define constants in the interface (interface). See the documentation for the interface for more examples.

After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.

Define and use a class constant

Copy Code code as follows:

<?php
Class MyClass
{
CONST CONSTANT = ' constant value ';
function Showconstant () {
Echo Self::constant. "\ n";
}
}

Echo Myclass::constant. "\ n";

$classname = "MyClass";
echo $classname:: Constant. "\ n"; After PHP 5.3.0

$class = new MyClass ();
$class->showconstant ();

echo $class:: Constant. " \ n "; After PHP 5.3.0
?>

Example #2 static Data sample

Copy Code code as follows:

<?php
class Foo {
After PHP 5.3.0
Const BAR = <<< ' EOT '
Bar
EOT;
}
?>

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.