top | item 4229125

Things to Check in PHP

7 points| jpro | 13 years ago |php.dzone.com | reply

7 comments

order
[+] Produce|13 years ago|reply
NO. If you are having performance problems then the first thing you need to do is to run a profiler against your code. If you're optimizing things because you think they might be slow, you're doing it wrong.

Not to mention how dense these suggestions are. Caching speeds things up? Really? Thank you for enlightening all of us.

[+] jpro|13 years ago|reply
I think it must be aimed at beginners in some of these points.
[+] nolok|13 years ago|reply
Site is slow ? Remove loops !
[+] pestaa|13 years ago|reply
A bonus tip: do not merge arrays if you're not absolutely positively sure they are truly associative. Consider:

    array_merge(array(CONST_1 => 'val1'), array(CONST_2 => 'val2'));
You'd expect the result to be

    array(CONST_1 => 'val1', CONST_2 => 'val2')
And you'd be wrong most of the time. If we have something like this:

    define('CONST_1', '1024');
    define('CONST_2', '65536');
The result of the above array_merge is:

    array(0 => 'val1', 1 => 'val2');
And that's because PHP doesn't care about the keys if they are numerical (note that I didn't say numbers; these were strings.)

Coming from PHP background first I didn't understand why I would need separate types for lists and dicts in Python. Now I think they are a godsend.

If I had the time I'd write 999 Things You Should Check in Your PHP Right Now. If this is the only language in your belt, please for your software's sake learn something else, widen your perspective.

[+] xd|13 years ago|reply
If this is the only language in your belt, please for your software's sake learn something else, widen your perspective.

Would help if you spent some time learning PHP in the first place :/ .. to preserve keys simply '+' the arrays together:

  php > define('CONST_1', '1024');
  php > define('CONST_2', '65536');
  php > print_r((array(CONST_1 => 'val1') + array(CONST_2 => 'val2')));
  Array
  (
      [1024] => val1
      [65536] => val2
  )
Also, constants have nothing to do with the problem you had, it's just the way array_merge works.

Edit: wow, just wow. The parent is voted up with miss information and I'm voted down for correcting him.

[+] MrEnigma|13 years ago|reply
Title makes it sound like security issues. But really it's just a list of 'improvements' you can make. Fun.