PHP 7.4 and a warning

Use of undefined constant is_front_page - assumed ‘is_front_page’ (this will throw an Error in a future version of PHP).
How to solve a warning: is_front_page

assign("level_page", "is_front_page");

What does your assign() function do? I can’t find a reference to it as a standard part of the language, though I might be missing something somewhere.

Do you mean define()?

Yeah, are you sure this is the line that’s throwing the error, and that you’ve put quotes around it in the published version?

This error would be generated if you had put assign("level_page", is_front_page);, but would not be generated by the code line you have provided us.

3 Likes

It it is Smarty design framework.
Please find the whole line. I’m sorry if I was not clear.

$level_page = (in_array($base_url, array( '', '/', '/index.php')) ? 'is_home' : 'is_front_page');
if($level_page == is_front_page){$smarty->assign("level_page", "is_front_page");} else {$smarty->assign("level_page", "is_home");}

I spy with my little eye a string that’s missing its quotes…

1 Like

Thank you for the warning.

That’s what PHP is telling you:

It’s saying “I ran into this thing is_front_page that is not a variable (cause it doesnt start with a $), it’s not a function (cause it’s not in my function table, and it doesnt have parenthesis after it), and it’s not defined as a constant (define("THREE",3); echo THREE;), so i’m going to assume it’s a string you forgot to put quotes around, and treat it as such.”

It is working now. I have modified code. Thank you.

I have another issue: How to solve this warning*?
Parameter must be an array or an object that implements Countable

    if(count($attachments) > 0){    }
elseif(...)
{    }

Whatever $attachments is, it’s not an array.

Try prefixing the if statement with the following:

echo gettype($attachments) 
// or 
var_dump($attachments);

Just for info, this change was made back in 7.2. I’m sure $attachments is NULL in your code when there are no attachments. count(NULL) used to work just fine returning the expected value of 0. The powers that be decided to ‘fix’ it and ended up ‘breaking’ quite a bit of existing code. Sometimes there is a fine line between fixing and breaking.

By the way, you might be surprised at how often pasting the error message into a browser’s search field reveals exactly what is going on.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.