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...!

Recommended Answers

All 9 Replies

Use This Code....

<?php

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

 ?>

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 ?

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>

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.

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...

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>

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

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....

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.