Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:14:21+00:00 2026-05-29T09:14:21+00:00

I am creating functions in my code to clean it up, one of them

  • 0

I am creating functions in my code to clean it up, one of them is for working out the offset for pagination.

The function is

function pageOffSet($page, $count, $resultsPerPage) {
     // WORK OUT HIGHEST PAGE NUMBER
     $pageCount = ceil($count / $resultsPerPage);
     // IF GET PAGE VARIABLE IS HIGHER THAN MAX PAGE POSSIBLE
     if (isset($page) && $page > $pageCount) {
          $page = $pageCount;
     // IF VALID GET PAGE VARIABLE
     } elseif (isset($page) && is_numeric($page) && $page > 1) { 
          $page = $page;
     // IF PAGE GET VARIABLE NOT SET
     } else { 
          $page = 1;
     }
     // OFFSET FOR POST RESULTS
     $offSet = ($page - 1) * $resultsPerPage;
     return array('offSet' => $offSet, 'resultsPerPage' => $resultsPerPage);
}

And I call it like,

$page = pageOffSet($_GET['page'], $count, 15);

Except $_GET['page'] may be set or unset? I check inside the function to see if it is set or not, but is it ok to do this? I get no errors but I just want to check?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:14:21+00:00Added an answer on May 29, 2026 at 9:14 am

    An error will not occur, but a warning will probably be generated, depending on your error reporting settings. However, a better approach may be using an default argument:

    function pageOffSet($count, $resultsPerPage, $page = NULL) { ... }
    

    Then to check if $page is there, use:

    if ($page)
    

    Or to check if it’s not there, use:

    if (!$page)
    

    This way, you can use the first two arguments without needing to provide the last; or you can provide all three. To use $page, check if it is NULL or not and use it accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've been creating functions for too long without taking my code to 'classes'. I
I am creating several image dynamically using the following code: function refresh_gallery(galleryidentifier, albumid) {
I'm working on creating one of those robot games. The user creates a robot
I have the following couple of C pre-processor macros for creating test functions: //
Are the old tricks (lookup table, approx functions) for creating faster implementations of sqrt()
Beyond creating a dll with all the same functions with the same interface and
Why does this attempt at creating a list of curried functions not work? def
When it comes to creating stored procedures, views, functions, etc., is it better to
I'm creating a function where I need to pass an object so that it
I'm creating a function in C to convert an index value into a string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.