the question title might be a bit confuse. but here is my case.
i would like my result to like something like:
0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
0000000001
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
0000000002
0000000020
0000000021
0000000022
0000000023
...
0000000003
...
0000000004
...
i can get the first numbers by doing this:
for ($i = 1; $i<=10; $i++){
$nr=str_pad($i, 10, "0", STR_PAD_LEFT);
echo $nr.'<br>';
}
but then how do i get the numbers in between?
any ideas?
Thanks
edit: the second row of numbers are based on 10 by 10 count.
so. first record will have 10 other records underneath, the second record another 10 and so on.
i've tried something using $floor= floor($i/10); and adding that to the second set of records in here: $nr=str_pad($floor, 10, "0", STR_PAD_LEFT);
I don't quite understand what you are trying to do. But one possible solution would be this:
for ($i = 1; $i < 100; $i++){
if ($i % 10 == 1)
echo str_pad(($i-1)/10, 10, "0", STR_PAD_LEFT) . "<br />\n";
echo ' '.str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
See it in action.
Depending on your problem, using two cycles might be better for you:
for ($i = 0; $i < 10; $i++){
echo str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
for ($j = 1; $j <= 10; $j++) {
echo ' '.str_pad($i*10+$j, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
}
Live.
This looks like homework, but I would change your loop as described to iterate the secondary numbers, and then use a test such as MOD ( http://php.net/manual/en/internals2.opcodes.mod.php ) to check for those times when you want to emit the first level grouping.
So you adjust your str_pad line to indent as well as left pad with 0, and add a lne near the top of the loop to check if ($i % 10 == 0) (that is, if it is divisible by 10 without a remainder).
Note this may not be the fastest nor most elegant way.
Related
I need a output like this
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And so on (There are more like this)
I used this code
<?php
for ($i = 0; $i <= 10 ; $i++) {
if ($i == 5 || $i == 4 || $i == 9) { //And so on Like this
echo "$i<br>";
}
}
?>
But the main problem is output is showing the number serially.
//It shows
Number 3
Number 4
Number 5
Number 8
Number 9
Number 10
//But I need
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And this takes much time to code. And it not looks so good. Sure there is a easy way out!
I am expecting something like this -
//Surely this is not right. It's just an idea.
<?php
$x = 5,4,9,3,8,10;
for ($i = 0; $i = $x; $i++) {
echo "$i<br>";
}
?>
Take this
$x = array(5,4,9,3,8,10);
foreach ($x as $i) {
echo "Number $i<br>";
}
but please, learn the basics of PHP if you really want to code in php.
JustOnUnderMillions's answer is corrent - You can even have access to key & value like this
$x = array(
"num1" => 1,
"num2" => 2,
...
...
);
foreach($x as $key => $value){
echo $key . " : " . $value . "<br>";
}
I would like to make a number sequential and repetitive to the beginning when it reaches the limit. For instance, the first number begins with 001, then 002, 003, and so on until it reaches 999. When it reaches 999, then the number will return to 01 and continues to repeat.
I currently have this loop:
$i = 001;
for ( $i; $i < 111; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
}
But how can I make that return to the first number and continues like before? I mean if the number reaches 999, it will back to 001, 002, 003, and so on.
Even if I don't quite understand why you want to do that, this is your solution :
<?php
for ($i = 001, $max = 111; $i < $max; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
if ($i == $max - 1) {
$i = 001;
}
}
But this will cause an infinite loop of course.
I want to get 3 int values in a for loop connected with each other. What I basically want is this:
000
001
002
003
010
011
...
323
330
331
I want to have each of the 3 numbers in a variable starting from 0 and max 3 when it gets higher than 3 it increases the number left to it by 1
For example
for($i = 0; $i <= $array; $i++){
echo $a . $b . $c . "<br />"; //Output would be the example I showed above
}
You can use base_convert to convert between arbitrary bases, in your case base 4.
for($i=0, $max = base_convert(333, 4, 10); $i < $max; ++$i) {
echo base_convert($i, 10 , 4);
}
To get 0-padded output, use printf with a format specifier:
printf('%03d', base_convert($i, 10 , 4));
for($i = 0; $i <= 63; $i++){
$c = $i % 4;
$b = ($i - $c)/4 % 4;
$a = (($i - $c)/4 - $b)/4;
echo $a . $b . $c . "<br />"; //Output would be the example I showed above
}
It's really just an explicit version of knittl's answer. The for loop has us step through every number from 0 thru 63, which happens to correspond with 0 through 333 in base 4. We then take $i, which is in base 10, and convert it to base 4 step by step. The % is the modulo operator - it returns the remainder after division. The least significant char is simply the remainder of $i/4, so we save that as $c. The next char is the 4 place (like the 10 place in base 10). So we subtract $c, which is already accounted for and divide by 4 and do the same thing.
for($a = 0; $a <= 3; $a++){
for($b = 0; $b <= 3; $b++){
for($c = 0; $c <= 3; $c++){
echo $a . $b . $c . "<br />";
}
}
}
Try this:
$count = 20; //how many numbers do you want
for($i =0; $i<$count; $i++) {
echo str_pad(base_convert($i, 10, 4),3,'0' , STR_PAD_LEFT) . '<br/>';
}
base_convert() converts every $i value from 10 base to 4.
str_pad() fill it with '0' to given length (3 here).
STR_PAD_LEFT means that zeros should be added on left side.
So I am trying to learn php, and the code for finding even numbers doesn't output anything, but I can't seem to find the error, can anybody find where I have made my stupid mistake? Here is the code:
<?php
/*this sets the array up with the data*/
$myarray = array(1,2,3,4,5,6,7,8,9,10);
/* this is the count to get the total number from my array */
$total = count($myarray);
?>
<h1>Display all even numbers</h1>
<ul>
<?php for ($i=1; $i < total; $i += 2): ?>
<li>The array element value is <?php echo $myarray[$i]; ?>. </li>
<?php endfor; ?>
</ul>
Thanks and if nobody wants to post an answer I understand new questions are frustrating.
thanks
Your code isn't finding even numbers. You're identifying their positions in the array, and printing values only for those indexes. Look at this php snippet.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
// Array indexes start at 0, not 1.
for ($i = 0; $i < count($myarray); $i++) {
echo "Index ", $i, ", value ", $myarray[$i], ": ";
// A value is even if there's no remainder when you divide it by 2.
if ($myarray[$i] % 2 == 0) {
echo "even\n";
}
else {
echo "odd\n";
}
}
?>
Put that in a file, and run it through php. You should see this.
Index 0, value 1: odd
Index 1, value 2: even
Index 2, value 3: odd
Index 3, value 4: even
Index 4, value 5: odd
Index 5, value 6: even
Index 6, value 7: odd
Index 7, value 8: even
Index 8, value 9: odd
Index 9, value 10: even
This shorter version will print only the even values.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
for ($i=0; $i < count($myarray); $i++) {
if ($myarray[$i] % 2 == 0) {
echo "Index ", $i, ", value ", $myarray[$i], "\n";
}
}
?>
Index 1, value 2
Index 3, value 4
Index 5, value 6
Index 7, value 8
Index 9, value 10
You're missing the $ variable creator in your for loop for total:
<?php for ($i=1; $i < $total; $i += 2): ?>
This code fits your problem.
<?php
$myarray = array(1,2,3,4,5,6,7,8,9,10);
$total = count($myarray);
echo "<h1>Display all even numbers</h1>";
echo "<ul>";
foreach($myarray as $rw)
{
if(($rw%2) == 0 ){
echo "<li>".$rw."</li>";
}
}
echo "</ul>";
?>
The modulus operator % is the best way to get the odd or even numbers in an array.
printing a even number in php. there $ais 20 when you run this code the out put is 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
$a = 20
for ($i=0; $i < $a; $i += 2)
{
echo "</n><br>".$i;
}
Out put like this
2
4
6
8
10
12
14
16
18
20
This code will divide the score until it reaches the number 5.
The $rows[score] is equal to 6600 in the database.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
echo $i = $i /2;
echo "<br>";
}
?>
This is what my browser outputs:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125
3.22265625
I don't understand why the browser output the last 3.22 - how do I stop the loop from echo out the last one that is less than 5??
Nothing wrong here the last value you get is from 6.4453125 / 2 = 3.22265625 since 6.4453125 still greater than 5
because 6 is higher than 5? so it does one more loop making $i 3.2 where the loop stops
If ($i<5) it wont go into the loop but there is no way to know until you check. $i = 6.4453125 the last time it checks, so it goes into the loop and it divides it by 2, which makes it less than 5 so it doesn't go into the loop again and stops.
I found the way to answer my own question so 3,22 will not be viewed on the page.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
$i = $i /2;
if($i >= 5) {
echo $i;
echo "<br>";
}
}
?>
Since you're dividing by two immediately before displaying the result, you want to stop your loop when $i >= (5*2) i.e. $i >= 10, not 5.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 10) {
echo $i = $i /2;
echo "<br>";
}
?>
This gives:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125