one liners Archives

Do something useful with one line of code

Date and time page last updated

A PHP one-liner to keep the date/time the page was last updated:

page last updated <?php echo date(“F d Y H:i:s”, getlastmod() ); ?>

Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let’s say the timezone is off by […]

Random web page background colors

If you want to create a random background color

<?php $hex = dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255)); ?>

and then you’d use the following in your body tag:

<body BGCOLOR=“#<?php echo($hex);?>”>

Related
How to change background color only on holidays

Is number odd or even?

Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn’t even necessary and could be expressed as a one-liner like this:

echo($number & 1); // $number = any integer, 0 = even, 1 = odd
 

Here’s the code to check […]