Your Ad Here

Why is === faster than == in PHP?

Written on March 9, 2010 – 1:53 am | by tihomir_wwf |

First, let’s remember the definitions of the operators: The Equal operator ($a == $b) returns TRUE when  $a is equal to $b. The Identical ($a === $b)  returns TRUE when $a is equal to $b, and they are of the same type. When we use the identical operator $a === $b, first it checks to [...]

Tags:

Common PHP Best Practices

Written on March 2, 2010 – 1:38 am | by tihomir_wwf |

After posts about php best practices in security and performance, here is a list of common php best practices: 1. Always use the standard php tags, ex. <?php echo “devtheweb.net blog”; ?> and never use shortcuts when declaring php code, ex. <?= echo “devtheweb.net blog”; ?> or <? echo “devtheweb.net blog”; ?> or even asp.net [...]

Tags:

public static or static public

Written on February 28, 2010 – 4:34 am | by tihomir_wwf |

Recently, I learned that in PHP and C# declarations like static public or static private are absolutely valid declarations of static methods. You can use both declarations, they has same meaning and there is no recommendation which one should be used: public static function myStaticMethod() or static public function myStaticMethod() But in my practice I [...]

Tags: ,

PHP Best Practices in Performance – Part 3

Written on February 23, 2010 – 1:22 am | by tihomir_wwf |

After the PHP Best Practices in Performance Part 1 and Part 2, here’s the last post from the series: 1. Instead of print(), use echo. It is a statement, so you avoid the function overhead of print(). 2. Incrementing a pre-initialized local variable is 9-10 faster than incrementing an undefined local variable. 3. Incrementing a [...]

Tags:

PHP Best Practices in Performance – Part 2

Written on February 16, 2010 – 1:33 am | by tihomir_wwf |

After PHP best Practices in Performance Part 1, here’s the next post from the series: 1. You can use array_keys() within foreach() when dealing with arrays. The reson is that foreach returns a copy of the array value. Using of array_keys will avoid excessive memory consumption, ex. foreach(array_keys($array) as $ak) { $v =& $array[$ak]; … [...]

Tags:

PHP Best Practices in Performance – Part 1

Written on February 9, 2010 – 6:31 am | by tihomir_wwf |

After PHP best practices in Security – Part 1 and Part 2, here’s the first post of the series about PHP best practices in Performance. 1. If you want to output a basic string, you’d better use single quotes, instead of double quotes. If you use a string surrounded by double quotes, it’s parsed by [...]

Tags:

Simple Rotating Ad Script in PHP

Written on February 7, 2010 – 4:12 am | by tihomir_wwf |

This weekend I sold banner ad space in one of my websites. The client wanted two banners to be rotated in same place. Here’s a simple php script how it can be done: <a href=”http://advertise-site-url.com” target=”_blank”> <?php $variable[1] = ‘<img src=”http://www.mysite.com/ads/banner-1.jpg” alt=”banner 1″ />’; $variable[2] =’<img src=”http://www.mysite.com/ads/banner-2.jpg” alt=”banner 2″ />’; $adCount = count($variable); $randomAdNumber = [...]

Tags:

PHP Best Practices in Security – Part 2

Written on February 2, 2010 – 1:24 am | by tihomir_wwf |

If your web server’s access permissions are wrong, it will be easier for somebody to take control over your server. So, next 3 advices are how to fix your access rights: 1. Do not allow PHP files to be writable. 2. Do not allow executable and writeable files in your web root. 3. Do not [...]

Tags:

PHP Best Practices in Security – Part 1

Written on January 26, 2010 – 2:00 am | by tihomir_wwf |

Here is the first post of PHP Best Practices Series. We’ll begin with (in my opinion) the most important aspect of web developing – the security. 1. Always Initialize Your Variables Let’s look at the following example: <?php if (check_user($_POST['username'], $_POST['password']) { $login = false; } if ($login) { forward_to_secure_page(); } ?> If the username [...]

Things You Probably Didn’t Know About PHP – Part 2

Written on January 19, 2010 – 2:30 am | by tihomir_wwf |

Here a new post about cool things in PHP that I found in my spare time. Here’s the list: 1. Use === and !== when you want to compare only variables with identical format, ex: $a = 1; $b = ’1′; $c = 1; $a == $b // true $a === $b // false $a [...]