Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Notice: Use of undefined constant type

Tags:

php

I'm doing something wrong and I can't figure out what to do about it (how to fix it)

code:

var_dump($each->promotion-type);

returns:

PHP Notice:  Use of undefined constant type - assumed 'type' in newfile.php on line 19

I can't change that variable name as that how I get from my vendor, any ideas how I can access that promotion-type variable? (syntax wise)

like image 651
alexus Avatar asked Oct 19 '11 01:10

alexus


People also ask

What is undefined constant in PHP?

Referring to a class constant without specifying the class scope.

How do you check if constant is defined in PHP?

Checking if a PHP Constant is Defined This can be achieved using the PHP defined() function. The defined() function takes the name of the constant to be checked as an argument and returns a value of true or false to indicate whether that constant exists.

How does PHP handle undefined index error?

Undefined Index in PHP is a Notice generated by the language. The simplest way to ignore such a notice is to ask PHP to stop generating such notices. You can either add a small line of code at the top of the PHP page or edit the field error_reporting in the php. ini file.


1 Answers

It's spitting that notice because the expression is being interpreted as variable $each->promotion minus constant type.

To access a property with the dash in its name, use curly braces and quotes:

var_dump($each->{'promotion-type'});
like image 112
BoltClock Avatar answered Oct 11 '22 17:10

BoltClock