Variable variable name in moodle code

Variable variable name in moodle code

by juan Diaz -
Number of replies: 4

hi there,

when i was looking at moodle code i found this piece of code at get_logs_usercourse in lib/datalib.php

$params['userid'] = $userid;
    $$coursestart = (int)$coursestart; // note: unfortunately pg complains if you use name parameter or column alias in GROUP BY
    //GESMOD-336 comentamos bloque y aniadimos bloque
    /*
    return $DB->get_records_sql("SELECT FLOOR((time - $coursestart)/". DAYSECS .") AS day, COUNT(*) AS num
                                   FROM {log}
                                  WHERE userid = :userid
                                        AND time > $coursestart $courseselect
                               GROUP BY FLOOR((time - $coursestart)/". DAYSECS .")", $params);

variable variables names are used in moodle code, but in this case ( $$coursestart ) i think it is a bug, nevertheless,  this code runs, because the variable is an autoasign and i think cast to int is for safety and rarely is needed.

I was misunderstood something or is it a bug?


Average of ratings: -
In reply to juan Diaz

Re: Variable variable name in moodle code

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Any code after // or between  /*  */ is commented and will never run.

In reply to juan Diaz

Re: Variable variable name in moodle code

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

That sure looks like a bug. Since $coursestart is a timestamp, there can be no reason to be using its value as a variable.