sumar 2 numeros pares in php

To sum two even numbers in PHP, you can use the modulo operator (%) to check if each number is even before adding them together. Here's an example:

main.php
$num1 = 4;
$num2 = 8;

if ($num1 % 2 == 0 && $num2 % 2 == 0) {
  $sum = $num1 + $num2;
  echo "The sum is: " . $sum;
} else {
  echo "Both numbers must be even.";
}
165 chars
10 lines

In this code, we're using two variables $num1 and $num2 to store the numbers we want to add. We're then using an if statement to check if both numbers are even using the modulo operator %. If both numbers have a remainder of 0 when divided by 2, then they are even and we can proceed with adding them together. If not, we display an error message.

If the numbers are even, we add them together and store the result in a variable $sum. We then use an echo statement to display the sum to the user. The output of this code for the given values of $num1 and $num2 would be:

main.php
The sum is: 12
15 chars
2 lines

gistlibby LogSnag