Tag Archives: PHP

PHP StartsWith and EndsWith functions with optional case senstive

It seems that the default PHP library doesn’t have an implementation for functions that tests for the existence of a sub string at the start or end of a larger string, so here they are:

Those functions accept an optional 3rd argument to choose whether to match the string with case sensitive or case insensitive comparison.

By default, the matching will be case sensitive.

Continue reading PHP StartsWith and EndsWith functions with optional case senstive

Unset an array item in a foreach loop

Trying to remove an item from a PHP foreach loop is not a straight forward job, because, PHP works on a copy of that array, so you need to do it like this:

$items = array('a','b','c','d');
foreach ($items as $key => $value) {
    if ($value=='b' || $value=='d') {
        unset($items[$key]);
    }
}
var_dump($items);

this outputs:

array(2) {
  [0]=>
  string(1) "a"
  [2]=>
  string(1) "c"
}

How to protect your PHP website against SQL injection

Mysql injection is a method to attack the database by inserting extra sql statements, that when executed could help an attacker do what he wants with the database.

Continue reading How to protect your PHP website against SQL injection