PHP syntax error when declaring a constant

ε祈祈猫儿з 提交于 2019-12-12 04:27:53

问题


The error I'm getting here is:

Parse error: syntax error, unexpected 'CASE' (T_CASE), expecting identifier (T_STRING) in [filename] on line 4

<?php

class PartCategories {
    const CASE = 1;
    const PROCESSOR = 2;
    const HARD_DRIVE = 3;
    const MEMORY = 4;
}

Can't say I've tried anything but for the life of me can't see what's wrong here.

Why is this a syntax error?


回答1:


case or CASE is already reserved by PHP for the switch statement. You can't redefine this as a constant, just as you cant define a const FUNCTION or something similar.

You're going to have to change the name of the constant.




回答2:


First of all you should know that PHP is not case sensitive. So "case", "Case", "caSE", "CAsE" and like are same thing.

And you already know that case is a php reserved keyword it can't be use for variable name.

You can initialize your class like $obj = new PartCategories();
or paRTCATEgories();
or PARTCATEGORIES();
mEANS tO sAY THAt PHp iS CaSE INSEnsitive.



来源:https://stackoverflow.com/questions/23932473/php-syntax-error-when-declaring-a-constant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!