0

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!

2
  • error says $x is undefined try to define the $x=0 before increment Commented Jul 15, 2013 at 15:32
  • There isn't an include() statement here. can you show more context which includes the include() call? Commented Jul 15, 2013 at 15:32

3 Answers 3

4

$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

Sign up to request clarification or add additional context in comments.

Comments

2

Declare

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

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.