How PHP defines array constants

Source: Internet
Author: User

Is that right?
<?php

Define (' Best_phper ', Array (' name ' = ' Gong wen ', ' address ' = ' China ');

My God, clearly told you not to be able to , reason is warning:constants may only evaluate to scalar values.

That is, the value of the define constant, only scalar and null are allowed. The type of scalar is integer, Float,string, or Boolean. So the array is not allowed

The usual practice is to use the following method to define "array constants" indirectly.

Method One: Using the eval () function
<?php
Define (' Best_phper ', ' Return array ' (' name ' = ' Gong wen ', ' address ' = ' China ');
$BEST _phper=eval (Best_phper);

Var_dump ($BEST _phper);

Method Two: Using the Json_encode () function
<?php
Define (' Best_phper ', Json_encode (' name ' = ' Gong wen ', ' address ' = ' China '));
$BEST _phper=json_decode (best_phper,true);

Var_dump ($BEST _phper);

Method Three: Adopt serialize () function
<?php
Define (' Best_phper ', serialize (' name ' = ' Gong wen ', ' address ' = ' China '));
$BEST _phper=unserialize (Best_phper);
Var_dump ($BEST _phper);

How PHP defines array constants

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.