Sum of numbers in PHP

Here we are again.
I’ve got this program which sould calculate the Sum/addition of the number introduced in the form.
When we click to get the result doesn’t give the result.
any suggestion…
thanks

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Without_Form</title>
</head>
<body>
   <form>  
Enter First Number:  
<input type="number" name="one" /><br><br>  
Enter Second Number:  
<input type="number" name="two" /><br><br>
Enter third Number:  
<input type="number" name="three" /><br><br> 
Enter Fourth Number:  
<input type="number" name="four" /><br><br> 
     
<input  type="submit" name="sum" value="sum" />  
</form>  
 </body>  
<?php
@$number1=$_GET['one'];   
@$number2=$_GET['two'];
@$number3=$_GET['three'];
@$number4=$_GET['four'];
@$sum=$_GET['Add'];
$sum = 0;
for ($i=$sum; $i <= $number4; $i++)     
{
  $sum++;
  

}
  echo "Sum of " . $number1 . " and " . $number2 . " and " . $number3 . " and " . $number4 . " is: ". $sum;


?>
</body>
</html>
</html> 
3 Likes

Some problems with your code besides the logic, which I’ll explain later:

  1. You can take the line @$sum=$_GET['Add'];, as $sum will be defined as equal to zero in the next line anyways.
  2. Wwhy are you putting @ before variable declarations? It suppresses errors, which may be okay for production code but you might want it for developing.

As I was saying before, the biggest reason why your code isn’t working is that the logic behind it is flawed. What you’re telling the computer to do is increment $sum $number4 times. $number1, 2 and 3 are not used at all in your code.
What you should do, in javascript (so you can’t copy and paste, rather think about it), is this:

let number1 = GET('one');
let number2 = GET('two');
let number3 = GET('three');
let number4 = GET('four');
let sum = 0;
sum += number1;
sum += number2;
sum += number3;
sum += number4;
console.log("Sum of numbers: ", sum)

Perhaps you were thinking of a better and more scalable way of doing it, like this?

let getters = ['one', 'two', 'three', 'four'];
let sum = 0;

for(let i=0; i < getters.length; i++){
    sum += _GET(getters[i]);
}

console.log(sum)

I wanted to include it just in case that was what you were thinking of, however, I would opt for the first method as of now, for starters

3 Likes

I am following one internet course and this is one of the challenges.
Is not for a school exam or something like that, just to let know.
Hi Nicolello,
that line you mention is one I added trying to work it out… everything. So, thank you for confirm that is an error.
I though the same about it was defined to zero in the next line but… you know… learn from my errors.
The reason I put the @ at the beginning of the variables is, for some reason in the course/ tutorial was mention to do it, but wasn’t mention wat was the purpose of it so, again… good to know.
I suspected that the number2 and number3 wasn’t used at that moment, probably I left it like that before to write the post for help, I tried lot of combinations and … well.
thanks

2 Likes

Please let me know if you figured it out, and don’t hesitate to ask should any questions arise :slight_smile:

1 Like

Hello,
does it mean ; let the variables (number1, number2 …) get the values of the input with the same name?
<input type=“number” name="one>
so when I write “1” in the input the variable will get the value… $number1 = 1
does this make sense to you…? :face_with_raised_eyebrow: :thinking:
because $sum = 0
sum += number1 will add the value of the number …$number1 to $sum
:confused: :confused:

1 Like

In HTML, an input tag can have a type property describing what kind of input to expect, like strings, integers, files, etc. In your case, you set it to type=number, which means only numbers are allowed.
In PHP, you can get values from $_GET by putting the same thing in the input’s name property.
If you were to write an input like so:

<input type="number" name="1"></input>

then to get its value in PHP you would need to use

$yourvariable = $_GET("1");

however, it is strongly discouraged to use numeric (1, 2, 3) name property values for <input> tags as it might break some things. I strongly advise using literal numbers (one, two, three, etc) instead.
see here, for example.
Please continue using

<input type="number" name="one"></input>

and

$yourvariable = $_GET("one");

As it is far better.
Hope this could help clear some confusion :slight_smile:

4 Likes

yes I understood about not to name with numerals

1 Like

good, I did it
but almost don’t understand

<head>
    <title>Without_Form</title>
</head>
<body>
   <form>  
Enter First Number:  
<input type="number" name="one" /><br><br>  
Enter Second Number:  
<input type="number" name="two" /><br><br>
Enter third Number:  
<input type="number" name="three" /><br><br> 
Enter Fourth Number:  
<input type="number" name="four" /><br><br> 
     
<input  type="submit" name="sum" value="sum" />  
</form>  
 </body>  
<?php
$number1=$_GET["one"];   
$number2=$_GET["two"];
$number3=$_GET["three"];
$number4=$_GET["four"];
$sum = $number1 + $number2 + $number3 + $number4;
for ($i=$sum; $i <= $number4; $i++)     
{
  // $number1;
  // $number2;
  // $number3;
  // $number4;

}
  echo "Sum of " . $number1 . " and " . $number2 . " and " . $number3 . " and " . $number4 . " is: "; echo $sum;


?>
</body>
</html>
</html> 

I have to say I tried something similar because it make sense that $sum = $number1+$number2…
I think I tried inside the curly braces, not sure now so didn’t work.
When I write only one number it gives me an error
Fatal error : Uncaught TypeError: Unsupported operand types: string + string in /home/runner/numbers/index.php:25 Stack trace: #0 {main} thrown in /home/runner/numbers/index.php on line 25

can you explain this?

3 Likes

Exactly! Summing the numbers directly is the best approach.
You can take the for loop, as it’s not doing anything.
Good job! :+1:

can you explain this?

Yes. The output from $_GET["one"] is "1", a string.
When you try summing two strings together… it doesn’t work, it will raise an error (like you said).
You must first transform them to integers, which can be done in two ways. You can choose how:

$integerNumber = intval($stringifiedNumber);
// or
$integerNumber = (int)$stringfiedNumber;

Can you integrate the logic needed to transform the strings into numbers?
If you can’t, come back here again.
Hope I was helpful :slightly_smiling_face:

1 Like

PHP supports C-style casts?

1 Like

According to this SO topic, yes
Also according to the php manual

2 Likes

Yes you can use (int) instead of intval(). However, (int) may produce unexpected results if the input value cannot be properly converted to an integer. So it is better using intval()

3 Likes

Do PHP casts work the same as C casts? (e.g. is it impossible to cast a number to a string? )

1 Like

Good question, you can use strval and (string) to convert an int to string (if my memory is not corrupted, I stopped using php some time back)

3 Likes

Another thing about the lines
Number=$_get[‘one’];

I changed the ’ for " and everything started doing things, like to give a result when pressed the button.
Is that ’ works in different ways?

Is giving me lots of warnings as well.
How can i fix this?
I now you said about the @ in front of some lines.

Could you please give me the new code with the edited for loop?

<head>
    <title>Without_Form</title>
</head>
<body>
   <form>  
Enter First Number:  
<input type="number" name="one" /><br><br>  
Enter Second Number:  
<input type="number" name="two" /><br><br>
Enter third Number:  
<input type="number" name="three" /><br><br> 
Enter Fourth Number:  
<input type="number" name="four" /><br><br> 
     
<input  type="submit" name="sum" value="sum" />  
</form>  
 </body>  
<?php

$number1=$_GET["one"];   
$number2=$_GET["two"];
$number3=$_GET["three"];
$number4=$_GET["four"];
$sum = $number1 + $number2 + $number3 + $number4;
  echo "Sum of " . $number1 . " and " . $number2 . " and " . $number3 . " and " . $number4 . " is: "; echo $sum;


?>
</body>
</html>
</html> 
I have to say I tried something similar because it make sense that $sum = $number1+$number2…
I think

What errors are you getting?
The for loop is not doing anything, you can take it out :slight_smile:

1 Like

I was thinking the same, what is the loop for ?

1 Like

I suppose when you start coding and dont have much knowledge you put things in which are doing nothing but you think they have to be there…
I didn’t understand what the loop was doing.
I did play around with it, changing values and variables and sometimes, something happened but because it wasn’t the result I expected i just left it there.

So, simplifying the code. Now loks a bit more readable.

2 Likes