Strange error: Undefined constant id - assumed 'id'

Strange error: Undefined constant id - assumed 'id'

by Miles Fletcher -
Number of replies: 2

Hello,

I am getting the course id for the loaded page in a Moodle block using:

$course = $DB->get_record('course', array(id => $_POST['pageID']), '*', MUST_EXIST);


It works, but it's giving a soft error in the log of:

Use of undefined constant id - assumed 'id' in /usr/local/moodle/blocks/page_status/block_page_status.php 


I don't see why "id" is being assumed as "id" here. It's not a big deal now as the code works but I worry it will eventually break in a future version if that assumption is no longer made.


I AM including config.php and MOODLE_INTERNAL is defined.

Average of ratings: -
In reply to Miles Fletcher

Re: Strange error: Undefined constant id - assumed 'id'

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers

It's because you have not put your array key inside quotes, so it doesn't realise it's a string and thinks it's a PHP constant.

array(id => $_POST['pageID'])
Should be

array('id' => $_POST['pageID'])

In reply to Conn Warwicker

Re: Strange error: Undefined constant id - assumed 'id'

by Miles Fletcher -

Huh. I just looked this up and you're right. I've worked with arrays and objects pretty much entirely in Javascript where they aren't inside speech marks as they're a type of variable. Thanks for clarifying.