Archive for the 'Programming Stuff' Category
[PHP] Uppercase & Lowercase
Another few functions for I find useful:
- ucfirst() – Make a string’s first character uppercase
- lcfirst() – Make a string’s first character lowercase
- strtolower() – Make a string lowercase
- strtoupper() – Make a string uppercase
- ucwords() – Uppercase the first character of each word in a string
From the looks of things, ucfirst() was coined from it’s function uppercase first letter (unlike some functions!). So goes for lcfirst() and ucwords(): lowercase first letter and uppercase first letter of each word. strtolower() lowercases every letter in every word in a string and strupper() functions in opposite, uppercases each letter in every word in a string.
ucfirst() examples:
<?php
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>
lcfirst() examples:
<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld
$bar = 'HELLO WORLD!';
$bar = lcfirst($bar); // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>
strtolower() examples:
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>
strtoupper() examples:
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>
ucwords() examples:
<?php
$foo = 'hello world!';
$foo = ucwords($foo); // Hello World!
$bar = 'HELLO WORLD!';
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>
[PHP] Rounding up and down
I just discovered a few cool functions:
- ceil() – Round fractions up
- floor() – Round fractions down
- round() – Rounds a float
I’m guessing ceil() is short for ceiling. When looking up, you have a ceiling whereas when looking down, you have a floor(). The functions’ names correspond to their actual function which makes it pretty easy to remember. And then we also have round which is just basic rounding.
ceil() examples:
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>
floor() examples:
<?php
echo floor(4.3); // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>
round() examples:
<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
<?php
echo round(9.5, 0, PHP_ROUND_HALF_UP); // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD); // 9echo round(8.5, 0, PHP_ROUND_HALF_UP); // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD); // 9
?>