Simple PHP Error I can't figure out

Hello,

I’m new to programming PHP and I’ve attempted to address a (what I like to think is simple) error in my php code. I can’t figure out why I’m getting this error over and over, it’s filling up my error log very fast. Any help appreciated. ;D

Error I am getting:
Undefined variable: IMG in C:\xampp\htdocs\otis15\otis15\public\jobs_dt.php on line 59
(doesn’t line 57 define the $IMG variable?

[php]
52 array(
53 ‘db’ => ‘DATE_CERTIFIED’,
54 ‘dt’ => 9,
55 ‘formatter’ => function( $d, $row ) {
56 if($d != ‘0000-00-00’){
57 $IMG = ‘Certified: '.$d.'’;
58 }
59 return $IMG;
60 }
61 ),
[/php]

Simply put, you are returning $IMG, which should be lowercase, whether it exists or not. Using the if statement, it is possible that variable doesn’t exist.

Ssaj, You have an “IF” statement that compares for something, but, if false, you do not set the results to
anything… So, something like:

if($d != ‘0000-00-00’){
$IMG = ‘Certified: '.$d.'’;
} else {
$IMG = ‘No results found…’;
}

Might work… You just can’t insert a variable that does not exist…

Also, I normally would do the function and “IF” clause outside the setting of a value inside an array.
Then, just use the returned value in the array… Easier to read, in my humble opinion…

Astonecipher??? Why no caps on the variable? I use caps sometimes? What am I missing there???

A variable in all caps, most will not actually see the $ when reading it, is used for constants. Kind of like whenever I see a class variable with an underscore _var in front, it means private.

IMG = ‘set_image.png’;
vs
$IMG = ‘changable_img.png’;

Just common naming conventions.

Ahhhh, yep, Astonecipher, naming conventions are nice, but, not everyone uses them the same way. I was
just wondering if I missed something… Thanks…

The biggest reason I say to always use lowercase is because Windows doesn’t care if something is upper or lower case. It sees it the same way Linux does not. It will just save you problems in the long run to always use lowercase.

Sponsor our Newsletter | Privacy Policy | Terms of Service