Wednesday, May 13, 2015

Ordinal Number

Here is a PHP function to get the ordinal number. Pass it a number, and it will return a textual number suffixed with either of "st" (like 1st), "nd" (like 2nd), "rd" (like 3rd), "th" (like 4th) etc.

<?php
/**
* ordinal_number.php - PHP function to get ordinal number.
* Pass it a number and it will return the number suffixed with either
* of "st" (like 1st), "th" (like 4th), "nd" (like 2nd), "rd" (like 3rd).
* Copyright (C) 2016 Prabhas Gupte
*
* This is free script: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should also see <http://www.gnu.org/licenses/gpl.txt>
*/
function ordinal_number($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if (($number % 100) >= 11 && ($number % 100) <= 13) {
$abbreviation = $number. 'th';
}
else {
$abbreviation = $number. $ends[$number % 10];
}
return $abbreviation;
} // ordinal_number
for($i=0; $i<20; $i++) {
echo '<br />' . ordinal_number($i);
}// for
?>

Grep - before and after

The grep is a command line utility for searching plain-text data sets for lines matching the given regular expression. And those using Linux (or some or other flavor of this OS), must have used this command. Here are two useful options of this command: -A for After and -B for Before. Both take a number as their value.

As their names suggest, -A gives you matching line and N lines after the matching line, similarly, -B gives you matching line and N lines before the matching line.

You can also combine both these options into single command. And, if your -A and -B values are same, then you can simply use -C option. C stands for Count.

# to get matching line and 5 lines before that
grep -B 5 "pattern" file
# to get matching line and 5 lines after that
grep -A 5 "pattern" file
# of course, you can combine -A and -B options together in single command. That's obvious.
# if your -A and -B values are same, you need not specify both the options. use -C instead.
grep -C 5 "pattern" file

Wednesday, May 6, 2015

PHP Blunder-2

Here is another 'oops!' moment for PHP. Do you remember the rule of transitivity? A related to B, and A related to C, implies that B is related to C. Right? Now apply this very understanding to the code below.
<?php
echo ("foo" == 0)?"foo equals 0\n":"foo does not equal 0\n"; // prints "foo equals 0"
echo ("foo" == true)?"foo equals true\n":"foo does not equal true\n"; // prints "foo equals true"
echo (true == 0)?"true equals 0\n":"true does not equal 0\n"; // prints "true does not equal 0"
?>
view raw awkward-php.php hosted with ❤ by GitHub
Now, foo equals true, and foo equals zero. So, according to this rule of transitivity, true equals zero?!