Home  >  Article  >  Backend Development  >  Note 018 constant() function in PHP

Note 018 constant() function in PHP

黄舟
黄舟Original
2016-12-26 10:12:261505browse

Syntax

mixed constant ( string $name )

Description

constant() function is used to return the value of a constant. This function is particularly useful when the name of a constant is not known in advance but the value of the constant needs to be obtained.

By passing the name of the constant to the $name parameter, you can get the value of the corresponding constant.

This function is still applicable to class constants.

Return value

Returns the value of the constant. If the constant is not defined, null is returned, but an E_WARNING level error will be generated.

Example

  <?php

    define("MAXSIZE", 100);    echo MAXSIZE;    echo constant("MAXSIZE"); // same thing as the previous line

    interface bar {        const test = &#39;foobar!&#39;;
    }    class foo {        const test = &#39;foobar!&#39;;
    }

    $const = &#39;test&#39;;

    var_dump(constant(&#39;bar::&#39;. $const)); // string(7) "foobar!"
    var_dump(constant(&#39;foo::&#39;. $const)); // string(7) "foobar!"

The above is the content of the constant() function in Note 018 PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn