-
Depending on the context and how often you write it in a certain program, it can be useful to have a function rather than any other sort of symbols in a program. If you use it in if statements, for example, it looks a lot simpler when skimming to have if(is_even($x)) than to write if($x%2===0), even though the result is the same. If you're planning to put that in 100 places in a program then I think it makes sense to use is_even rather than %...===0. Of course if you're only using it once, that's rather silly. Much like naming certain properties (such as in CSS) with a reasonable name for their use, a well-suitably named function can make the code appear a lot smoother than a bunch of math symbols, much like CSS should not contain a number of properties named "prop1," "prop2," ... "propn."
-
On the contrary: if ($x % === 0) is exactly the sort of place where there is no point in having a function. It is useful in languages that don't have a way of treating an operator as a function to wrap operators in functions if one is intending to pass them to higher-order functions, but apart from that there is no reason.
What you suggest, functions as documentation, is again usually a good idea, but this particular instance is so common that, like addition, there's simply no gain. Any programmer worth their salt will recognise it instantly.
-
I agree with Twey.
Once you get to something so short such as:
Code:
return $x % 2 === 0;
It is unreasonable for a function, and it is completely comprehensible.
-
Just for clarity, the three equals signs (===) do mean that both operands have to be the same type as well as equalling the same value right?
For example:
1 == "1" // Would return true?
and
1 === "1" // Would return false?
-
-
Correct.
PHP Code:
<?php
function single($n) {
return $n == 5;
}
function dbl($n) {
return $n === 5;
}
echo single('5');
echo dbl('5');
?>
-
So, I did something wrong here the even function was working but then it stopped after I added in a check for a new month and it now brings up the only accepts integers error.
Maybe too many date functions in on process (I have 4 date functions)? I'm using php4 also if that makes a difference.
I'm switching servers in the next few days anyone know if a small orange company is good or now of a good one to use? I have 3 domains I need to swap over. My current server IX webhosting has crashed my domains 3+ times today and denies it's happening (2 techs, 1 said he knew) so I can't stay with them.
It also did occur to me that I would need a way to get the php to write even when the page was not loaded so that at the new month point it would write regardless of if a visitor was on the page at 11:59:59 or not.
PHP Code:
$date2 = date('m.j.y');
function is_even($int) {
if ( !is_int($int) ) {
trigger_error('The is_even() function only accepts integers.', E_USER_WARNING);
return;
// Or get rid of trigger_error(xxx) and replace it with: return false;
// to accept any input. This will cause odd numbers and decimals
// to return false.
}
$result = $int % 2 == 0 ? true : false;
return $result;
}
$int = date('j');
if ( is_even($int) ) {
$first = '<tr class="even"><td>';
} else {
$first = '<tr class="odd"><td>';
}
$newmonth = date('m.d H:i:s');
if ($newmonth == "1/31 23:59:59" || $newmonth == "2.28 23:59:59" || $newmonth == "2.29 23:59:59" || $newmonth == "3.31 23:59:59" || $newmonth == "4.30 23:59:59" || $newmonth == "5/31 23:59:59" || $newmonth == "6/30 23:59:59" || $newmonth == "7/31 23:59:59" || $newmonth == "8/30 23:59:59" || $newmonth == "9/30 23:59:59" || $newmonth == "10/31 23:59:59" || $newmonth == "11/30 23:59:59" || $newmonth == "12/31 23:59:59") {
$who = "<tr><td class=\"row\" colspan=\"3\"> NEW MONTH</td></tr>" . $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>\n";
} else {
$who = $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>\n";
}
$where = str_replace("</table>", $who, $red);
$logger = fopen($info, "w+");
fwrite($logger, $where);
fclose($logger);
Thanks for any ideas and thoughts you can offer with these questions as well:).
-
Probably you had a string like '2' or a float like 2.4. You don't actually want to only accept integers here, because of PHP's weak typing: just accept whatever you're given. Remove that clause from the function entirely.
Code:
$result = $int % 2 == 0 ? true : false;
return $result;
is just pleonastically redundant and redundantly pleonastic, and can be written return $int % 2 == 0;.
-
$int = date('j');
The date() function returns a string. So you have two options, either make, $int an integer before using it in the even() function, or change the function.
You can make the result of the date function an integer by doing this:
PHP Code:
$int = (int) date('j'); // typecast date('j') to an integer
Or, you can use Twey's function, but remember it will accept strings and return true.
PHP Code:
// $int = "this is a string"; will return true
function is_even($int) {
return $int % 2 == 0;
}
-
Oh yea that makes sense thanks again.
Twey I just saw your response, won't the date always be a whole integer?