Note 018 constant() function in PHP

黄舟
Release: 2023-03-04 09:22:01
Original
1457 people have browsed it

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!"
Copy after login

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 (m.sbmmt.com)!


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!