Several examples of the new characteristics of PHP5.6 const

Source: Internet
Author: User
Tags constant scalar


PHP5.6 Const new Attribute definition class constants can use constant scalar expressions (Constant scalar expressions), for example:


<?php

Class MyTimer {
Const SEC_PER_DAY = 60 * 60 * 24;
}

?>

The difference between define and const is that define can be used to define global constants, and const is a constant that defines the class.


The difference between static and Define,const is that static-defined variables can be changed, then both are not, and static static variables are initialized directly in memory with the class, and can be used directly, such as $oneclass::hobby.

Can define define an array? For example define (' A_array ', ARRAY (' O ' => ' ooo ', ' x ' => ' xxx ')).

Not before PHP5.6, but you can serialize the array by serialize, such as:


# define constant, serialize array
Define ("FRUITS", Serialize (Array ("Apple", "cherry", "banana"));

# Use it
$my _fruits = unserialize (fruits);

After PHP5.6, you can define an array directly by const:

Const Default_roles = array (' Guy ', ' development team ');

Or:

Const Default_roles = [' Guy ', ' development team '];

If it is PHP7, you can define the array directly with define:


Define (' Default_roles ', array (' Guy ', ' development Team '));

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.