Hi I am unable to get a solution for the below program.
<?php
function is_even($param1)
{
if (($param1 % 2) == 0)
return TRUE;
else
return FALSE;
}
$num = 5;
echo 'The number is a' . is_even($num);
?>
This is actually not returning anything.
How do I call this function for me get the string" The number is odd"
Please help.
Problem with functions
Moderator: General Moderators
This should do it.
You are returning either true or false, so we can test this value and set our variable based on it. Another option would be to return strings from the function (instead of returning boolean TRUE, return string "even" etc.), but this would make the function less flexible.
Another way might be to use the ternery "if":
EDIT: Tidied up the syntax in the is_even function if statement.
You are returning either true or false, so we can test this value and set our variable based on it. Another option would be to return strings from the function (instead of returning boolean TRUE, return string "even" etc.), but this would make the function less flexible.
Code: Select all
function is_even($param1)
{
if (($param1 % 2) == 0) {
return TRUE;
} else {
return FALSE;
}
$num = 5;
$outString = "";
if(is_even($num)) {
$outString = "even";
} else {
$outString = "odd";
}
echo "The number is $outString";Code: Select all
function is_even($param1)
{
if (($param1 % 2) == 0) {
return TRUE;
} else {
return FALSE;
}
$num = 5;
echo "The number is ".(is_even($num) ? "even" : "odd");