Category Archives: PHP

PHP – Prevent GIF image loaded from browser cache

I would like to show a GIF image on a website but i found that the animation is not working as the browser caches the GIF image after first load. Here is a PHP workaround by adding the current timestamp to the image src url.

<img src="https://eureka.ykyuen.info/example.gif?v=<?php echo Date("Y.m.d.G.i.s"); ?>" />

 

Done =)

Reference: cache woes, how to force an image to refresh or load fresh

PHP – String startsWith and endsWith

The following 2 functions could check if a string starts/ends with another string.

/* Return TRUE if $needle is empty */
function startsWith($haystack, $needle) {
  $length = strlen($needle);
  return (substr($haystack, 0, $length) === $needle);
}

/* Return TRUE if $needle is empty */
function endsWith($haystack, $needle) {
  $length = strlen($needle);
  if ($length == 0) {
    return TRUE;
  }
  $start  = $length * -1;
  return (substr($haystack, $start) === $needle);
}

Please note that if the $needle is empty, both functions will return TRUE.

Done =)

Reference: StackOverflow – PHP startsWith() and endsWith() functions

PHP – Write an Array/Object to File by print_r()

In debugging PHP program, i always use print() and print_r() to output the variable content to the browser. But this does not work when we are not using browser. A simple way to get the content is to write it to a file.

I found a nice post written by Ivan which provides this simple way with just a few lines of code.
Ivan Kristianto – [TIPS] Write print_r() Output To File Continue reading PHP – Write an Array/Object to File by print_r()