Constants in PHP

Source: Internet
Author: User
Tags constant define function include reference require

PHP defines several constants in advance and provides a mechanism to define them at run time. Constants and variables are basically the same, and the difference is that constants must be defined with the Define function, and once a constant is defined, it cannot be redefined.

Pre-defined constants in PHP:

__file__
This default constant is the name of the PHP program file. If a reference file (include or require) is in the reference file, the constant is the reference file name, not the file name.

__line__
This default constant is the number of PHP program lines. If a reference file (include or require) is in the reference file, the constant is the row that refers to the file, not the file line that references it.

Php_version
This built-in constant is a version of the PHP program, such as ' 3.0.8-dev '.

Php_os
This built-in constant refers to the operating system name, such as ' Linux ', that executes the PHP parser.

TRUE
This constant is the truth (true).

FALSE
This constant is a pseudo value (false).

E_error
This constant refers to the nearest error.

E_warning
This constant refers to the nearest warning.

E_parse
This literal is a potential problem for parsing syntax.

E_notice
This constant is unusual but not necessarily wrong. For example, to access a variable that does not exist.

These e_ are the beginning of the constants, you can refer to the error_reporting () function, there are more relevant instructions.


You can define more constants with the Define function.

For example, define constants:

<?php
Define ("CONSTANT", "Hello world.");
Echo CONSTANT; Outputs "Hello world."
?>

With examples of __file__ and __line__

Php:
function Report_error ($file, $line, $message) {
echo "An error occured in $file to line $line: $message."
}

Report_error (__file__,__line__, "something went wrong!");
?>


My own wording:
?
$file = __file__;
$line = __line__;
Echo $file;
echo "<br><br>";
Echo $line;
echo "<br><br>";
Echo __file__;
echo "<br><br>";
Echo (__line__);
?>

ECHO constant with Echo (); Without echo ""



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.