w3resource

PHP Exercises: Check two given integers, and return true if one of them is 30 or if their sum is 30

PHP Basic Algorithm: Exercise-3 with Solution

Write a PHP program to check two given integers, and return true if one of them is 30 or if their sum is 30.

Sample Solution:

PHP Code :

<?php
// Define a function named "test" that takes two parameters, $x and $y
function test($x, $y) 
{
    // Use the logical OR operator to check if $x is equal to 30, $y is equal to 30, or the sum of $x and $y is equal to 30
    return ($x == 30) || ($y == 30) || ($x + $y == 30);
}

// Use var_dump to print the result of calling test with arguments 30 and 0
var_dump(test(30, 0));

// Use var_dump to print the result of calling test with arguments 25 and 5
var_dump(test(25, 5));

// Use var_dump to print the result of calling test with arguments 20 and 30
var_dump(test(20, 30));

// Use var_dump to print the result of calling test with arguments 20 and 25
var_dump(test(20, 25));
?> 

Sample Output:

bool(true)
bool(true)
bool(true)
bool(false)

Visual Presentation:

PHP Basic Algorithm Exercises: Check two given integers, and return true if one of them is 30 or if their sum is 30.

Flowchart:

Flowchart: Check two given integers, and return true if one of them is 30 or if their sum is 30.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to compute the sum of the two given integer values. If the two values are the same, then returns triple their sum.
Next: Write a PHP program to check a given integer and return true if it is within 10 of 100 or 200.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.