How to use an associative array with PHP’s str_replace function

If you are a PHP developer, I’m sure you know the str_replace function for replacing strings and are probably aware that you can pass an array of strings as the search strings and an array as the replacements. What this post looks at is a way of using a single associative array instead of two arrays, which makes dealing with a large number of replacements much more maintainable in your code.

Categories PHP

PHP Exceptions – available information

This post is part of a series about PHP exceptions and exception handling; the first post gave a basic overview of how they work and this one shows the information that is available from the exception object when an exception is caught.

Categories PHP

Extend PHP’s exception object

PHP’s Exception object can be extended (just as any class can be that is not declared final) which means different exception types can be thrown that handle the exception differently.

Categories PHP

Replace error reporting with exception handlers with PHP

As part of a series of posts about PHP exception handling, this post looks at how to make regular errors use exception model. Normally only the newer object-oriented extensions throw exceptions but it is possible to make all errors throw an exception instead using set_error_handler.

Categories PHP

How to escape variables with PHP PEAR DB

When using the PHP PEAR DB database abstraction library it is important to escape variables just as it is when using the native database function calls. If you don’t know why this is then do a search using your favourite search engine for "sql injection". This post shows the PEAR DB way of escaping variables.

Categories PHP

Get the value of a PHP constant dynamically

It is possible to get the value of a PHP constant dynamically using the constant() function. It takes a string value as the parameter and returns the value for the constant if it is defined.

Categories PHP