开发者

Is this efficient looping PHP coding?

开发者 https://www.devze.com 2023-02-27 02:40 出处:网络
I have just started learning coding and PHP so I have been looking at practising what I have learnt so far however am unsure how efficient or inefficient my coding is. I would appreciate your comments

I have just started learning coding and PHP so I have been looking at practising what I have learnt so far however am unsure how efficient or inefficient my coding is. I would appreciate your comments on the loop below. It is a simple loop and have commented out a simpler example of it. I would appreciate any advise on how I can better my coding.

//Initialize page
    $startpa开发者_Go百科ge = isset($_POST['page']) ? $_POST['page'] : 1;
    $endpage = 11;

    //Loop through the start and end of the page
    while($startpage < $endpage) {

        if(isset($_POST['submit'])) {
            $startpage=$startpage+1;
        }

        if($startpage < $endpage) {
            break;
        }
    }

    //Alternative option
    // if(isset($_POST['submit'])) {

        // if($startpage < $endpage) {
            // $startpage = $startpage + 1;
        // }    
    // }

EDIT

The reason I am performing a $_POST check is because I only want have the user move from one page to the next after the hit submit.


With what you have coded, no, it's terribly inefficient ;p. you can technically replace your code with

if(isset($_POST['submit']) && $startpage < $endpage){
    $startpage = $endpage;
}

since your logic isn't actually doing anything but increasing $startpage until it is equal to $endpage. As a tidbit, to increment by one you can do $startpage++;

Your comment //Loop through the start and end of the page also... should not be there because it doesn't make any sense, it's definitely better off to not have a comment if your comment doesn't really make sense of what's going on IMO. It kind of explains what you're trying to do, but I'm still confused :P.

Like everyone else is also saying, you seem to not really understand how a while loop works, but if you can assert otherwise I take it back :).

Here's my explanation of a while loop.

A while loop repeats itself as often as the condition you put next to it is true. So, if you say while($number is less than 11) and then increase $number by one everytime, you don't need to break; from the loop when $number is less than 11 manually, since that's what loops automatically do.


I'm not entirely sure what you're trying to do, but it looks like a for loop would be more readable.

$startpage = isset($_POST['page']) ? $_POST['page'] : 1;
$endpage = 11;
for($i=$startpage; $i<$endpage; $i++)
  {
  // do something
  }

Perhaps it's personal preference, but while is more useful when you want to loop through an array of results, or when you want to keep checking the result of another function before doing something else.

Comments on your code specifically:

  1. You can rewrite $startpage=$startpage+1; as $startpage++;
  2. isset($_POST['submit']) will give you the same answer on every run through the loop, so set the result as a variable before you run the loop.


I'm not sure what you're trying to do here. That loop will run only once because of the if statement. If you're trying to print the page numbers from $startpage to $endpage, you can simple do this:

$startpage = isset($_POST['page']) ? $_POST['page'] : 1;
$endpage = 11;

while($startpage < $endpage) {
    $startpage=$startpage + 1;
}

Actually, why are you checking $_POST['submit'] here ?

0

精彩评论

暂无评论...
验证码 换一张
取 消