Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
anybody can help me me with this? I have a variable such:
$page = '50';
$newpage = 'http://www.mydomain.com/page/'.$page.'';
I want new page echo such this:
http://www.mydomain.com/page/50
http://www.mydomain.com/page/49
......
.......
....... until page acho such:
http://www.mydomain.com/page/1
<?php
for($page=50;$page>0;$page--) {
$newpage = 'http://www.mydomain.com/page/'.$page.'';
echo "$newpage\n";
}
?>
Look at this for loop:
$prefix = 'http://www.mydomain.com/page/';
for ($page = 50; $page >= 1; $page--) {
echo $prefix.$page;
}
First, the variable $page is initialized. I use 50 instead of '50' because we're dealing with numbers and not with strings.
The next bit is the condition while the loop continues: $page >= 1 - so the loop will stop after 1.
The last part is a decrement operator, it subracts 1 off $page for each loop iteration.
Finally, the prefix and the page number are combined using the concat operator (.).
What you are looking for is :
<?php
for($i=50; 0 <= $i; $i--){
echo $i.'<br />';
}
?>
if you don't want to go upto 0, change <= for <. This will loop from 50 until 0 so it will eacho in order :
50
49
48
47
46
...
3
2
1
0
Here 50 is your start number, 0 is your last number to go. So in this example it print out 50 until 0.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to print like this using loop without nested loop:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
first line start from 1 and print only one integer
second line start from 2 and print two integer
third line start from 4 and print four integer
fourth line start from 8 and print eight integer
(same condition in other line if exist)
A pair of counters should do the trick;
$threshold = 1;
$x = 1;
for($i = 1; $i <= 15; $i++) {
echo $i . ' ';
if($x == $threshold) {
echo "<br>\n";
$threshold = $threshold * 2;
$x = 0;
}
$x++;
}
Will keep on working till infinity, or your script runs out of memory. Whatever comes first :)
You can achieve with single while and some array functions. Try this.
<?php
$start=1;
while($start<=10){
$array = range($start,($start+$start-1));
echo implode(' ',$array)."<br>";
$start=$start*2;
}
?>
something like this?
$i=1;
$t=1;
while($i<1000)
{
if($i==$t)
{
if($t!=1) echo "<br />";
$t=$t*2;
}
echo $i." ";
$i++;
}
same thing but with log($i,2).
<?php
$i=1;
while($i<20)
{
echo $i;
$i++;
if(filter_var(log($i,2), FILTER_VALIDATE_INT))
echo PHP_EOL;
else
echo "\t";
}
You can even do it without any for and some recursion ;-)
<?php
$elements = range(1,15);
display($elements);
function display(array $parts, $step = 1) {
if(! count($parts)) {
return;
}
$elements = array_splice($parts, 0, $step);
echo implode("\t", $elements)."\n";
display($parts, $step * 2);
}
See the working code on https://eval.in/755277
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to implement this method to my webpage. I want to echo the same sentence random times every time someone refresh my php webpage.
For example: Hello World!
You can do:
<?php
$min = 1; // Minimum 1 time
$max = 10; // Maximum 10 times
$string = "Hello world!\n"; // Print Hello World! and a new line
$x = rand($min,$max); // Make the random
$i = 0; // Set the iterator
do {
echo $string; // Echo the text
$i = $i + 1; // Increment the iterator
} while ($i < $x); // Test to make sure we didn't do it too many times
?>
This echos "Hello world!" anywhere from 1 to 10 times. You can search the PHP API to see how this works.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my solution to Euler Project Problem 14
<?php
$count = 0 ;
$max = 0;
for($n = 2 ; $n < 1000000 ; $n++){
while ($n > 1)
{
if ($n % 2 == 0 )
{
$n = $n/2;
}
else
{
$n = 3*$n + 1 ;
}
$count += 1;
if($count > $max )
{
$max = $count;
$final = $n;
}
}
}
echo $final;
>?
It took so long to run.I looked some other solutions and they were very similar to my code logically,but they were running way too faster than mine.
My question is,what is it that makes my code inefficient? What am I missing here?
Thanks ^^
Your approach is straight forward and unpolished.
You can use dynamic programming in order to improve the runtime (complexity approach).
Let's say you want to compute for 5. You will have :
5 -> 16 -> 8 -> 4 -> 2 -> 1.
But if you do that you will also have computed the value for 8 and 16.
The idea is to store the value that you have already computed in order to save work when you will need them later.
Working on the complexity of an algorithm will be the key of some problems, so better get used to it early.
An other reason why it is slow, is the choice of the language. For example try with C it will run a lot faster.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to display total number from start to end in a range in php .Each time number will increase by 10 .
For example total number is 35
Then number will display this way :
1 to 10
11 to 20
21 to 30
31 to 35
$num=35;
for($i=1;$i<$num;$i=$i+10)
{
$j=$i+9;
if($j>$num)
$j=$num;
echo $i.' to '. $j;
}
try
$num=35;
for($i=1;$i<=$num;$i++) {
if($i %10 == 0)
echo $i.'<br>';
else
echo $i;
}
will output :-
12345678910
11121314151617181920
21222324252627282930
3132333435
Use a for loop like this
for($x = 1; $x <= 35, $x += 10){
echo $x . " to " . $x+9;
}
This will work i hope.
Thanks
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this codes. My question is How i can remove .1 one from rest of numbers in my output.
Its gives : The counter is 1464.1
I want it to give : The counter is 1464
<?php
$i = 1000;
$t = 10;
while ($i < 100000000) {
echo"<p>The counter is $i";
$i = $i + ($i / $t);
}
?>
And output is :
The counter is 1000
The counter is 1100
The counter is 1210
The counter is 1331
The counter is 1464.1
The counter is 1610.51
Use intval:
echo '<p>The counter is '.intval($i) .'</p>';
EDIT:
Display purposes only use the above code.
Data manipulation use:
$i = intval ($i + ($i / $t));
Use intval():
$i = intval( $i + ($i / $t) );
You can type cast it when you echo it.
echo "<p>The counter is " . (int)$i . "</p>";
You can use:
echo "<p>The counter is ".floor($i)."</p>";
or
echo "<p>The counter is ".intval($i)."</p>";
These will effectively 'cut off' the decimal (or give you the integer value)
or you can round to the nearest integer:
echo "<p>The counter is " . round($i) . "</p>";
OR if you are trying to subtract .1 from your answer (it is unclear in the OP without more examples) you can do this:
echo "<p>The counter is ".($i - .1)."</p>";