0

I need a little help with an if statement in php. I'm trying to set a variable called offset according to a page that I am loading in WordPress. Here's the variable:

$offset = ($paged * 6);

What it does is it loads the first page, which is:

http://example.com/blog

and $offset is thus set to 0 because $paged is referring to the appending number on the URL. The second page, for example is:

http://example.com/blog/2/

which makes $offset set to 12. The problem is, I need the second page to define $offset as 6, the third page to define $offset as 12, etc. I tried using:

$offset = ($paged * 6 - 6)

which works except on the first page. On the first page it defines $offset as -6. SO, I wanted to create an if statement that says if $paged is equal to 0 then $offset is equal to 0, else $offset is equal to ($paged * 6 - 6).

I struggle with syntax, even though I understand what needs to be done here. Any help would be greatly appreciated. Thanks!

4 Answers 4

4

Because this is two different cases which cannot be easily integrated into a single formula, use an if statement:

if ($paged == 0)
  $offset = 0;
else
  $offset = ($paged - 1) * 6;

You can write this shorter using the ternary operator, but I think the above if statement is more readable:

$offset = ($paged == 0) ? 0 : ($paged - 1) * 6;
Sign up to request clarification or add additional context in comments.

Comments

0

An alternative:

if($paged == 0) $paged = 1;
$offset = ($paged - 1) * 6;

or

$offset = ($paged) ? ($paged - 1) * 6 : 0;

2 Comments

Pages 0 and 1 will be the same... :D
@jadkik94 yep but what is the problem with that?
0

you can use the following one line code

$offset = max(($paged - 1) * 6, 0);

4 Comments

The max() solution, while clever, may be confusing for a future maintainer.
yes and no... it will also make him learn a simple trick. :-)
There's often a conflict between "simple tricks" and "maintainability". Otherwise everybody would be happy Perl coders.
you are absolutely right. I am actually big fan of tricks and smart coding. Thats why I oftenly use ternary operator wherever I can. Some may prefer to use if-else instead of ternary.
0

You could use: $offset = max(0, $paged * 6 -6);. Which takes the maximum of 0 and the other value. If the other value is negative, then 0 will be the result.

Now, to use an if statement, this is how you would do it:

$offset = $paged * 6 - 6;
if ($offset < 0) {
    $offset = 0;
}

Or:

if ($paged == 0) {
    $offset = 0;
} else {
    $offset = $paged * 6 - 6;
}

And you might want to use a ternary operator for this simple case:

$offset = ($paged == 0)? 0: ($paged * 6 - 6);

(which does the exact same thing as the above)

Note: You could replace 6*a-6 = 6*(a-1), but it's of no importance in the code, just readability...

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.