2

I have written a module to display images on my site, but I'm getting the following error on the site:

Fatal error: Can't use method return value in write context in /public_html/camps/modules/mod_slider/mod_slider.php on line 24

This is what is showing on line 24:

<?php if (!empty($params->get('image-1')) || !empty($params->get('image-1-alt')) || !empty($params->get('image-1-caption'))) { ?>

Any ideas what's gone wrong?

The server is running PHP version 5.3.10-1ubuntu3.15

5

Prior to PHP 5.5, the function empty() can only be used on variables, not return values, so you have to approach it like this:

$image = $params->get('image-1');
$alt = $params->get('image-1-alt');
$caption = $params->get('image-1-caption');
if (!empty($image) || !empty($alt) || !empty($caption)) {
  • Beat me to it. Good answer. +1 but no flowers – Lodder Dec 5 '14 at 14:24
  • 1
    lol...bought a Poinsettia yesterday for my wife, but WAS thinking of you, lol. And nice to know about PHP5.5 (still on 5.4 AND 5.2 here) – GDP Dec 5 '14 at 14:25
4

Just a little addition to @GDP's answer.

Instead of checking whether the variables are empty or not, you could set default values for them like so:

$image   = $params->get('image-1', 'images/image.png');
$alt     = $params->get('image-1-alt', 'Generic Alt');
$caption = $params->get('image-1-caption', 'Generic Caption');

Then simply create a generic image that will show is nothing has been set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.