0

Hey guys!

I am a beginner of php. so i don't know that which will be the best as a loop? While ? or For ?

Can anyone help me please.... I want to do this...!

Just count 1 to 10 and then add it...

i mean 1+2+3+4+5+6+7+8+9+10

nothing much....

Please help me...!

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by JRM
1

Use This Code....

<?php

$total=0;
for($a=1;$a<=10;$a++)
	$total=$total+$a;
echo "Total of 1 to 10 is : $total";

 ?>
0

ok! so according to you it's a better way to use for loop! ok..

but now i want to ask the use to enter any two values from where to start and where to stop summing....

have any idea ?

0

I made an html file named test1.htm and enter the bellow code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
<form method="POST" action="test2.php">

Enter first value where to start : <input type="text" name="first"><br>
Enter second value where to end  : <input type="text" name="second"><br>

<input type="submit" name="submit" value="Submit">
</form>

 </BODY>
</HTML>

Edited by mitalichauhan: code mistake...

0

for loops tend to be best used when you know how many iterations you need while the while loop works best when you don't know how many iterations you will need off the start.

1

I dunno, post like this disturb me a bit , since you are not thinking at all, but just ask others to do the work for you.
anyway, here is the the way you set the start and end:
Modifying the previous answer:..

<?php
var $start = 3 ;
var $end = 30 ;
$total=0;
for($start;$start<=$end;$start++)
	$total=$total+$a;
echo "Total of 1 to 10 is : $total";

 ?>

Now...was that very difficult?
I set the variables as hard values, but you can also make a form with inputs for those variables, then pick them up with $_POST.
No, I am not going to write the form for you...

Edited by JRM: error

1

okey then make one more file named test2.php and enter following code...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <?php

$first=$_POST['first'];
$second=$_POST['second'];
$total=0;
for(;$first<=$second;$first++)
	$total=$total+$first;
echo "Total of $first to $Second is : $total";

 ?>
</BODY>
</HTML>
0

harshbavaliya, you are right! i want the value of start and end from the user... and your code is working.....

thanks.... thanks for solving my problem....

1

JRM he/she want to get value of start and end from user... so your code will not work Sorry Dear !

Ah, but it was YOUR code that YOU wrote for him/her.
He/she did not need to think his/her way out of a paper bag! LOL

I believe in assisting the process, not doing it for them.
You have been snookered into doing somebody's homework, my friend!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.