Efe
Efe

Reputation: 954

Notice: Undefined variable: x in include()

I used ternary operators to have multiple backgrounds with listed item.

But now I am getting this error, Error message

Notice: Undefined variable: x in include() (line 45 of /home/content/67/11380467/html/beta/sites/all/modules/custom/blogs.tpl.php).

<?php 

    $x++;   
    $class = ($x%2 == 0)? 'second': '';
    print $class; 

?>

Can you please help me understand what went wrong here and help me fix it.

Thanks!

Upvotes: 0

Views: 880

Answers (3)

user2584140
user2584140

Reputation:

The problem is with $x++ as the included file doesn't know about $x assuming you have declared it somewhere else.

if you haven't declared $x anywhere, then it is probably a good idea to declare it $x = 0;

Alternatively, you can ignore the Notice and everything should work fine because PHP is a weakly typed language, it will auto-initialize it, but in general it is bad practice to depend on something like that. Notices are not necessarily a bug, but they usually point to one.

Upvotes: 2

srakl
srakl

Reputation: 2619

Declare

$x = 0; before $ x++; first

Upvotes: 2

Rene Pot
Rene Pot

Reputation: 24815

$x is undefined. You can't do $x++ when you didn't define it yet. You probably need to add this in front:

$x = 0;

This is assuming you want to start at 0

Upvotes: 4

Related Questions