Definition of php constants

Source: Internet
Author: User
{Code ...} here, the constant FRUIT can be defined as [& #039; apple & #039;, & #039; orage & #039;] (this is an array ), shouldn't FRUIT be a number or string? Please answer !!
class testClass {    const FRUIT = ['apple','orage'];}

Can the constant FRUIT be defined as ['apple', 'orage']? (this is an array.) shouldn't FRUIT be a number or string? Please answer !!

Reply content:
class testClass {    const FRUIT = ['apple','orage'];}

Can the constant FRUIT be defined as ['apple', 'orage']? (this is an array.) shouldn't FRUIT be a number or string? Please answer !!

A constant is a constant that cannot be changed. it does not have to be a number or a string.

PHP7 defines an array constant PHP70new-featres through defind ()

Eg:

    define('ANIMALS', [        'dog',        'cat',        'bird'    ]);echo ANIMALS[1]; // outputs "cat"`

You can also remove defind () from PHP7 and use const to define a constant array.

In addition, PHP7 also supports operations between constants. for example:const A = 1; const B =3; const C = A + B;

From PHP7, you can use define to define a constant array:
define('APP1', array(1,2,3)); var_export(APP1);
In earlier versions of PHP, you can first serialize the array into a string and then define a constant. when using PHP, you can deserialize it:
define('APP2', serialize(array(1,2,3))); var_export(unserialize(APP2));
PHP can also use const to define a constant array from 5.6:
const APP3 = array(1,2,3); var_export(APP3);

Const means that the variable definition cannot be changed. after the array is defined as const, the elements in the variable cannot be added or decreased.
Before 5.6.0, php does not have such a feature, which can be used at this time.

/** @const */private static $myArray = array(...);

.
5.6.0 began to add this feature, so this code has certain requirements for the php version of the machine to run.

It seems that this definition can be defined in the class from around 5.6.

Class SomeClass {const FRUIT = ['apple', 'orage'];} // Obtain echo SomeClass: FRUIT [0]; // apple

However, some IDEs do not support this function.

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.