Archive for the ‘php’ Category

02
Jul

I am working a lot with PHP, and also developing on a live CMS. Because there are already people using the system i don’t want errors to appear.

So, i tried to find a way to fix that. And, i found it. Ofcourse I did, because else this post wouldn’t excist.

This is the row that sets a new default error handler function:

$old_error_handler = set_error_handler("myErrorHandler");

And this is the function:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
   // Do something with the vars you get from this function.
  // $errno = Error number. Every type of error has an number ;)
  // $errstr = text to explain the error. The one you always see by default
  // $errfile = File the error occurred in
  // $errline = Line number in $errfile the error occurred at.
}

Check for more information www.php.net/set_error_handler

, , , , ,

04
Jun

Recently I was talking about ksort() to sort array, but i forgot to mention the reversed method. (link)

krsort($ray);

This function sorts an array reversed. So Z – A instead of A – Z.

, , ,

31
May

A problem I encountered recently was, after a short search on the internet, easy to fix.
I was trying to arrange an array in php by it’s keys, but I didn’t know how at first. This is my array

$ray = array('2' => 'second', '1' => 'first', 'a' => 'alpha);

As you noticed the order is 2 – 1 – a. But i want to arrange it to 1 – 2 – a. The only thing I had to do was this:

ksort($ray);

Thats all you need to do. And it will arrange the array by key.

, , ,

21
May

Something i recently discovered, and already regret I didn’t knew this before, makes it much more easier to parse $_POST or $_GET variables into ordinary variables so i can use it properly for mysql insertions.

This is what i used to do:

<?php
   $var1 = mysql_real_escape($_POST['var1']);
   $var2 = mysql_real_escape($_POST['var2']);
   $var3 = mysql_real_escape($_POST['var3']);
?>

Ofcourse we all can see the repeat in this, but i never figured out how to solve that issue.
But ofcourse there is always someone else with a better idea, and I like to share it with you

<?php
  foreach ($_POST as $key => $value){
    $$key = mysql_real_escape_string($value);
  }
?>

And thats simply all. It will put all the $_POST array items into a numerous variables.
The double “$” does what you expect it to do, it makes a variable out of the content of the variable.

When $a = test, than $$a = test2 will result into $test = test2.
It is that easy!

, , , , ,