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.php165 chars10 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.php15 chars2 lines
gistlibby LogSnag