Jump to content

PHP Loops for Newbe


johann75

Recommended Posts

Hello, I am new in php and have made only little so fare. I am trying to make a script, but the script stops. I dont know how loops work.

<?php

$lenkzeit = 4.5;

$lenkunter = 1;

$ruhezeit1 = 11;

$ruhezeit2 = 9;

$stunden1 = 0;

 

$km = 1000;

$kmh = 80;

 

$stunden0 = $km/$kmh;

$stunden01 = $stunden0;

 

while ($stunden01 > $lenkzeit){

$stunden1 = $stunden1 + $lenkzeit + 1;

$stunden01 = $stunden01 - $lenkzeit;    //Stops here

 

if ($stunden01 > $lenkzeit) {

$stunden1 = $stunden1 + $lenkzeit + $ruhezeit;

$stunden01 = $stunden01 - $lenkzeit;

}

}

if ($stunden01 < $lenkzeit) {

$srunden1 = $stunden1 + $stunden01;

$stunden01 = 0;

}

echo $srunden1;

?>

Link to comment
Share on other sites

A typical loop:

 

$a = 1;

$b = 99

while ($a <= $b):

    echo $a;

    $a++;

endwhile;

 

 

You don't seem to have an endwhile statement in your loop.  I would suggest doing a little reading on the web.

 

ie:  google ---  While Loops in PHP and you will find 10000 examples if not more.

 

Angelo

 

Link to comment
Share on other sites

You don't seem to have an endwhile statement in your loop.  I would suggest doing a little reading on the web.

 

ie:  google ---  While Loops in PHP and you will find 10000 examples if not more.

 

Angelo

 

The OP's form is correct also. See here

 

Add these lines to the beginning of your script and tell us the error

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

Thank you DevilsAdvocate. There was one error. The variable $ruhezeit should be $ruhezeit1. The while loop is working now, but when the while is false, the rest of the script is not running, the last IF.  When the while is false, is jumps right to the ECHO without working through the last IF.

Link to comment
Share on other sites

You don't seem to have an endwhile statement in your loop.  I would suggest doing a little reading on the web.

 

ie:  google ---  While Loops in PHP and you will find 10000 examples if not more.

 

Angelo

 

The OP's form is correct also. See here

 

I just like to see fully qualified and expanded code, I am not one for shortform even if it is accepted.

:)

 

 

Add these lines to the beginning of your script and tell us the error

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

When the while is false, is jumps right to the ECHO without working through the last IF.

 

That would mean the condition in the if isn't being met. I have no idea what language that is or what those are supposed to mean so that's as much as I can tell you  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.