PHP if else with modulo number - php

I have number from 1 until 200, number will be printed in a page, each page has a limit by 81. When reach 81 then print into the next page.
This is output i want:
number = 200;
print number 1 - 81 in page 1
create new page
print number 82 - 162 in page 2
create new page
print number 163 - 200 in page 3
How to do it in PHP?
Update:
I mean i just want to print like above. No need of size of paper.
Here's what i have done
$number = 200;
$limit = 81;
$page = ceil($number/$limit);
for($i = 0; $i < $page; $i++){
echo "print number 1 - ". 200-81 ." in page".$i; // i'm stuck in here
}

I think this might work:
$number = 200;
$limit = 81;
$page = ceil($number/$limit);
for($i = 0; $i < $page; $i++){
$start = $i*$limit+1;
$last = ((($i+1)*$limit));
$last = $last>$number?$number:$last;
echo "<br>print number $start - $last in page".($i+1);
}

Related

PHP: for loop $i value turns zero if the number is greater

I want to turn the $i variable value to start counting from 1 if the given value is greater than 10:
here is what i am trying to achieve
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i < $givenValue; $i++) {
if ($givenValue > 10){
$i = 1;
}
echo $i."<br>";
}
?>
This is how i want my result to look like
output: 1
output: 2
output: 3
output: 4
output: 5
output: 6
output: 7
output: 8
output: 9
output: 10
output: 1
output: 2
output: 3
output: 4
output: 5
in for loop body
Any help is welcome
You can use modulo calculation to get the result you want.
I also changed your if from $givenvalue to $i as $givenvalue will "always" be 10+.
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($i > 10){
Echo $i%10 . "\n";
}else{
echo $i . "\n";
}
}
https://3v4l.org/5afc5
Another option, if that is possible for you, is to start at zero and only use modulo calculation and add one to it to get the same result.
This also means I need to stop the loop at <$givenvalue as your original code shows.
$givenValue = 15; //number of x value
for ($i = 0; $i < $givenValue; $i++) {
Echo $i%10+1 . "\n";
}
https://3v4l.org/r0sgA
A method that uses less looping is to add 10 to the loop on each iteration and create the values using range().
Then add them to the array with array_merge, and output with implode.
$givenValue = 47; //number of x value
$breakpoint = 10;
$arr=[];
For($i = $breakpoint; $i< $givenValue;){
// Add new values from 1-$breakpoint in array
$arr = array_merge($arr, range(1,$breakpoint));
$i +=$breakpoint;
}
// Loop will exit before all values been collected
// Add the rest of the values
$arr = array_merge($arr, range(1,$givenValue-($i-10)));
// Echo the values in array
Echo implode("\n", $arr);
https://3v4l.org/jGsO4
Your code can be written like this:
<?php
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++)
{
if ($i > 10)
{
$i = 1;
$givenValue-=10;
}
echo "output: $i\n";
}
?>
http://sandbox.onlinephpfunctions.com/code/ed34d8dcd12a9a5a866b73338ad1209f55298519
You are resenting the counter, I would expect the behaviour you have. To do what you want add another counter to the mix
$j=1;
$givenValue = 15; //number of x value
for ($i = 1; $i <= $givenValue; $i++) {
if ($j > 10){
$j = 1;
}
echo $j."\n";
++$j;
}
You also had several missing ;
Output:
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
If you want to end on 5 you have to do 16 as the $givenValue or change it to <= less than or equal
See it here live
See what I have now, the $i variable counts to the $givenValue then the $j variable counts along side it, but with a range of 1-10 ( resets to 1 after 10 )

PHP Dubble For Loop

I need to make a loop in php that says 1*9 = 9 2*9 = 18 and 1*8 = 8 and 1*7 = 7 etc in loop so the result will be this:
1*7 = 7
2*7 = 14
3*7 = 21
1*8 = 8
2*8 = 16
3*8 = 24
1*9 = 9
2*9 = 18
3*9 = 27
And I need to use a nested loop for that i tried some things out but i cant get it to work here is what is did. But for the most part i don't really understand what nested loops do and what there used for. hope you can help me thanks!
for ($tafel7 = 0; $tafel7 <= 9; $tafel7++) {
for($tafel7 = 0; $tafel7 <= 9; $tafel7++){
for($tafel7 = 0; $tafel7 <= 9; $tafel7++){
$antwoord9 = $tafel7 * 9;
echo "$tafel7 x 8 = $antwoord9 <br>";
$antwoord8 = $tafel7 * 8;
echo "$tafel7 x 8 = $antwoord8 <br>";
$antwoord7 = $tafel7 * 7;
echo "$tafel7 x 7 = $antwoord7 <br>";
};
};
};
Your question is not as clear but if you just want the exact result, here's the code:
for($i = 7; $i <= 9; $i++){
for($j = 1; $j <= 3; $j++){
$result = $i * $j;
echo $j." * ".$i." = ".$result."<br>";
}
}
If you need to print complete tables, then try this:
for($i = 1; $i <= 9; $i++){
for($j = 1; $j <= 10; $j++){
$result = $i * $j;
echo $i." * ".$j." = ".$result."<br>";
}
}
As far as the explanation is concerned, then listen:
A normal loop works by executing a block of statement(s) again and again until a condition is met.
So, nested loops are basically loops in another loop. This means that the block of code will be executed 'n' number of times by the inner loop, and then the inner loop will again be executed by the outer loop, again 'n' number of times.
'n' means whatever the number of times the conditions are met for the respective for loops.
Hope you got my explanation!
You need to loop over your two variables, (1) the left operand and (2) the right operand
for ($right = 7; $right <= 9; $right++) {
for ($left = 1; $left <= 3; $left++) {
$product = $left * $right;
echo "{$left} x {$right} = {$product} <br>";
}
}
In this example, I have to loops one with the times table you want eg 7, 8 and 9 "tafel" and the other one with how many times you want it to run for each times table "$hoeveel". This can easily be altered to more times tables or how many times you want to run, you also only need to export the values once as it cycles through the loops anyways. Hope it helps.
for ($tafel = 7; $tafel <= 9; $tafel++) {
for ($hoeveel = 1; $hoeveel <= 3; $hoeveel++) {
$antwoord = $tafel * $hoeveel;
echo "$tafel x $hoeveel = $antwoord <br>";
}
}

How to write a loop that generates range intervals in PHP?

I'm newbie here. I want to ask about for loops in PHP.
How to write code that will output this:
i =1;
j = 0-20;
then if i=2; j = 20-40,
i=3; j=40-60,
and so on.
Notes: j is range data from 0-(+20);
I have no idea how to start.
i = 1; range = 0-20;
i = 2; range = 20-40;
i = 3; range = 40-60;
...
You wrote the pattern yourself: "range data from 0-(+20)". You have 3 variables there, i and range defined by min and max. You need to take some time thinking how each of these will change based on how i will change. Then it shouldn't be hard to come up with something like this:
$rangeSize = 20;
for ($i = 0; $i < 10; ++$i) {
$rangeMin = $i * $rangeSize;
$rangeMax = $rangeMin + $rangeSize;
echo "$i : $rangeMin - $rangeMax" . PHP_EOL;
}
output:
0 : 0 - 20
1 : 20 - 40
2 : 40 - 60
3 : 60 - 80
4 : 80 - 100
5 : 100 - 120
6 : 120 - 140
7 : 140 - 160
8 : 160 - 180
9 : 180 - 200
Should be something along those lines
refer to http://www.w3schools.com/php/php_looping_for.asp for the official documentation
<?php
$max_i = put the maximum value for i here
for ($i = 0; $i <= $max_i; $i++)
{
echo "i = $i <br>";
echo "j = $i*20 - 20 <br>";
}
?>

PHP infinity loop [While]

I am having some problems with PHP.
I used while to sum a number's digits always that it has more than two digits, some how, it gets into an infinity loop.
e.g: 56 = 5 + 6 = 11 = 1+1= 2.
Here is the code:
$somaP = 0;
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Can anyone help me? Guess it is a simple mistake, but I couldn't fix it.
You need to reset the value of $somaP in your while loop.
Currently it continues to increase its value every time through the loop.
Try this:
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
$somaP = 0;
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Take a look at this line:
$numPer = (string) $somaP;
It seems that the length of $somaP is never lesser (or equal) than 1. So the length of $numPer is never lesser (or equal) than 1.
What are you trying to do?
It's unclear to me.
This for example would add every number in a string together?
E.g "1234" = 1+2+3+4 = 10
$total = 0;
for($i = 0; i < strlen($string); $i++){
$total += $string[$i];
}
echo $total;
This looks cleaner I would say:
$numPer = 56;
while ($numPer > 9){
$numPer = array_sum(str_split($numPer));
}
echo $numPer;
PHP handles all string <> number conversions for you, so no need to do (string) on a number unless really needed.

Simple PHP program requires less time to execute

i had applied for a job recently and the requirement was to complete a test and then interview
2 questions were given for test which was very simple and i did it successfully but still i was told that i have failed the test because the script took more than 18 seconds to complete execution. here is the program i dont understand what else i could do to make it fast. although i have failed the test but still wants to know else i could do?
Program language is PHP and i had to do it using command line input
here is the question:
K Difference
Given N numbers , [N<=10^5] we need to count the total pairs of numbers that have a difference of K. [K>0 and K<1e9]
Input Format:
1st line contains N & K (integers).
2nd line contains N numbers of the set. All the N numbers are assured to be distinct.
Output Format:
One integer saying the no of pairs of numbers that have a diff K.
Sample Input #00:
5 2
1 5 3 4 2
Sample Output #00:3
Sample Input #01:
10 1
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793
Sample Output #01:
0
Note: Java/C# code should be in a class named "Solution"
Read input from STDIN and write output to STDOUT.
and this is the solution
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");
fscanf($fr, "%d", $total_nums);
fscanf($fr, "%d", $diff);
$ary_nums = array();
for ($i = 0; $i < $total_nums; $i++) {
fscanf($fr, "%d", $ary_nums[$i]);
}
$count = 0;
sort($ary_nums);
for ($i = $total_nums - 1; $i > 0; $i--) {
for ($j = $i - 1; $j >= 0; $j--) {
if ($ary_nums[$i] - $ary_nums[$j] == $diff) {
$count++;
$j = 0;
}
}
}
fprintf($fw, "%d", $count);
Your algorithm's runtime is O(N^2) that is approximately 10^5 * 10^5 = 10^10. With some basic observation it can be reduced to O(NlgN) which is approximately 10^5*16 = 1.6*10^6 only.
Algorithm:
Sort the array ary_nums.
for every i'th integer of the array, make a binary search to find if ary_nums[i]-K, is present in the array or not. If present increase result, skip i'th integer otherwise.
sort($ary_nums);
for ($i = $total_nums - 1; $i > 0; $i--) {
$hi = $i-1;
$low = 0;
while($hi>=$low){
$mid = ($hi+$low)/2;
if($ary_nums[$mid]==$ary_nums[$i]-$diff){
$count++;
break;
}
if($ary_nums[$mid]<$ary_nums[$i]-$diff){
$low = $mid+1;
}
else{
$hi = $mid-1;
}
}
}
}
I got the same question for my technical interview. I wonder if we are interviewing for the same company. :)
Anyway, here is my answer I came up with (after the interview):
// Insert code to get the input here
$count = 0;
sort ($arr);
for ($i = 0, $max = $N - 1; $i < $max; $i++)
{
$lower_limit = $i + 1;
$upper_limit = $max;
while ($lower_limit <= $upper_limit)
{
$midpoint = ceil (($lower_limit + $upper_limit) / 2);
$diff = $arr[$midpoint] - $arr[$i];
if ($diff == $K)
{
// Found it. Increment the count and break the inner loop.
$count++;
$lower_limit = $upper_limit + 1;
}
elseif ($diff < $K)
{
// Search to the right of midpoint
$lower_limit = $midpoint + 1;
}
else
{
// Search to the left of midpoint
$upper_limit = $midpoint - 1;
}
}
}
#Fallen: Your code failed for the following inputs:
Enter the numbers N and K: 10 3
Enter numbers for the set: 1 2 3 4 5 6 7 8 9 10
Result: 6
I think it has to do with your calculation of $mid (not accounting for odd number)

Categories