~$ php -r "var_dump( filter_var( "false", FILTER_VALIDATE_BOOLEAN ) ); "
bool(false)
~$ php -r "var_dump( filter_var( "true", FILTER_VALIDATE_BOOLEAN ) ); "
bool(true)
~$ php -r "var_dump( filter_var( 0, FILTER_VALIDATE_BOOLEAN ) ); "
bool(false)
~$ php -r "var_dump( filter_var( 1, FILTER_VALIDATE_BOOLEAN ) ); "
bool(true)
Flush the APC cache via shell command
~$: php -r "apc_clear_cache();"
PHP-Doc: enclose multiple properties with one docblock
/**
* Single property.
* @var string
*/
private $property1 = 'foo';
/**#@+
* Multiple properties enclosed by the same docblock.
* @var boolean
*/
private $property2 = true;
private $property3 = false;
/**#@-*/
/**#@+
* Multiple properties enclosed by the same docblock.
* @var integer
*/
private $property4 = 12;
private $property5 = 22;
/**#@-*/
Calculate the dimension best fit into the max size (PHP)
// Set original size as default return dimension.
$dimension = new MyDimension($width, $height);
// Check if original size is bigger than max size.
if ($width <= $maxWidth && $height <= $maxHeight) {
return $dimension;
}
// We need to calculate the maximum best fit dimension.
// Calculate orignal and maximum ratio.
$originalRatio = $width/$height;
$maxRatio = $maxWidth/$maxHeight;
// Based on the ratios, calculate new width and height
if ($maxRatio < $originalRatio) {
// Best fit ratio for width
$dimension->setWidth($maxWidth);
// Only if ratio is on, height must be calculated on ratio
if ($regardRatio) {
$dimension->setHeight(round($maxWidth/$originalRatio));
}
} else {
// Best fit ratio for height
$dimension->setHeight($maxHeight);
// Only if ratio is on, width must be calculated on ratio
if ($regardRatio) {
$dimension->setWidth(round($maxHeight*$originalRatio));
}
}
return $dimension;
Calculate the age of something (PHP)
$endDate = strtotime((null === $endDate) ? 'now' : $endDate);
$startDate = strtotime($startDate);
$age = 0;
while ($endDate > $startDate = strtotime('+1 year', $startDate)) {
++$age;
}
return $age;