A thread for sharing little functions (100 lines or less) that you use.
This first function is an enhanced version of PHP’s native empty() function in that it behaves in a more intuitive manner. Prior to PHP 5.3 if you create an object and don’t set the magic __isset() function empty could return true even when the value is false.
In addition, with arrays it’s often useful to know if all the values of the array are empty even if the keys have been set. It also returns true if you pass it a multidimensional array where all the values are empty or are arrays of empty values.
function isEmpty( $val ) {
if (is_array($val)) {
foreach ( $val as $value ) {
if ( is_array($value)) {
$return = isEmpty($value);
} else {
$return = empty( $value );
}
if (!$return) {
return false;
}
}
return true;
} else {
return empty($val);
}
}
So who else has a small function they’d like to share?
My day job is programming Perl, which has this function built in, so for me it is not ambiguous at all… It does exactly the same, echo with a line break.
Guessing that Larry wall, creator of Perl, employee of NASA and Linguist invented this, I don’t think you can call it stupid
And an other note, I don’t use <p> or <br> tags, I use a templating system, so I never echo something out to the web. This is simple in what it does… it is not ment for production…
Don’t jump to conclusions, and don’t call other people’s opinions stupid, everybody diserves respect.
You aren’t escaping $_SERVER[‘REQUEST_URI’] – SQL injection! Also, if $userid is an integer, it shouldn’t have quotes around it (only MySQL allows this, so it will break if you change to Pg or something else).
include_javascripts_timestamped() is especially useful IMO, because you can set a far-future expiry date and not have to care about browsers not updating when they need to.
// There is also a include_stylesheets_timestamped(), but I'll leave that out
function include_javascripts_timestamped()
{
$web = /* get the web dir from config */;
$javascripts = get_javascripts();
$html = '';
foreach ($javascripts as $file)
{
if (substr($file, 0, 7) === 'http://')
{
$ts = false;
}
elseif ($file[0] === '/')
{
$ts = filemtime($web.$file);
}
else
{
$ts = filemtime($web.'/js/'.$file);
}
$file = $file.($ts ? '?ts='.$ts : '');
$html .= '<script type="text/javascript" src="'.$file.'"></script>'."\
";
}
echo $html;
}
/**
*
* @param BaseRecord $obj
* @return string <a> tag
*/
function link_to_record(BaseRecord $obj)
{
switch (get_class($obj))
{
case 'Member':
return '<a href="'.url_for_record($obj).'">'.$obj->getUsername().'</a>';
case 'Story':
return '<a href="'.url_for_record($obj).'">'.$obj->getTitle().'</a>';
default:
throw new InvalidArgumentException('Can\\'t link to '.get_class($obj));
}
}
/**
*
* @param BaseRecord $obj
* @param array $options
* @return string The URL to use in links
*/
function url_for_record(BaseRecord $obj, array $options = array())
{
switch (get_class($obj))
{
case 'Member':
$route = '@profile?username='.$obj->getUsername();
break;
case 'Story':
$action = (isset($options['action']) ? $options['action'] : 'index');
$route = '@story?id='.$obj->getId().'&action='.$action;
break;
default:
throw new InvalidArgumentException('Can\\'t make URL for '.get_class($obj));
}
return url_for($route, $options); // change this to however you create URLs in your own code
}
This function replaces the technique of inserting echo or print_r in your code followed immediately by exit. It can be overloaded so you can check as many or as few variables as you need. After printing out those variables with print_r or echo (as appropriate) it then does a print_r of the $_GET var, $_POST var, Debug Strack and the included files.
One other wrinkle is that if your code hasn’t sent output yet the trace function will send the headers needed to put the browser in text mode to make reading the print_r results easier.
<?
/**
* Find a string in a string. Unlike the native function, this returns
* boolean only. Also if the needle is an array each value of the array
* is considered for a match and return true if a match occurs.
*
* @param string $haystack
* @param mixed $needle
*/
function stringInString( $haystack, $needle ) {
if (is_array($needle)) {
foreach ($needle as $value) {
if (stristr($haystack, $value)) {
return true;
}
}
}
return stristr($haystack, $needle) ? true : false;
}?>
Why not use [fphp]str_replace[/fphp] or [fphp]str_ireplace[/fphp] directly? The savings on typed characters doesn’t justify the overhead of the function.
Salathe got the other examples where a native function works just as well.