Since there are many coders around here, it'd be nice to have a place where useful code snippets towards web go.
So yes, anything that has to do with webcode and is useful in other projects then yours, could be posted here.


Check if a number is even or odd, instead of using the php MOD (%). This should have a higher performance.
Code:
<?php
	
function even_or_odd($num)
{
	return ($num&1) ? "odd" : "even";
}

Month number to the month name using php.
Code:
<?php
	
function month_name($num)
{
	return date("F", mktime(0, 0, 0, $num, 1));
}
Age verification
Code:
<?php
  
function allowed( $y, $m, $d, $min ) {
    $age = date("Y") - $y - (( date("md") < $m.$d ) ? 1 : 0);
        return ( $age >= $min ) ? true : false;
}
 
/* Example usage: */
 
if ( allowed($_POST['year'], $_POST['month'], $_POST['day'], 18) )
{
    /* allowed */
}
else
{
    /* not allowed */
}
Post preview function
Code:
<?php

function previewText($text, $len, $end = "...")
{
    return ( $length < strlen($text) ) ? substr($text, 0, $len).$end : $text ;
}
 
 
/* example usage */
 
echo previewText("Lorem ipsum dolor sit amet, consectetur adipisicing elit", 15);
 
/* Output: Lorem ipsum dol... */