4 January 2010 View Comments

Practical PHP Testing – Free Ebook

Practical PHP Testing – Free Ebook

If you’re new to testing in PHP, and were wondering how to create and run automated tests, here’s an ebook which will help you get the basics right.

Practical PHP Testing is an ebook which is a compilation of  articles from Giorgio Sironi’s blog on Practical PHP testing.

This book takes you though the basics of PHPUnit – how to install it and start writing simple tests using PHP Unit. Here are some of what this ebook covers:

  • bonus chapter on TDD theory;
  • a case study on testing a php function;
  • working code samples, some of whom were originally kept on pastebin.com;
  • sets of TDD exercises at the end of each chapter;
  • glossary that substitutes external links to wiki and other posts, to not interrupt your reading with terms lookup.

More information and download link is available here.

Reblog this post [with Zemanta]

18 July 2008 View Comments

mbstring Functions by default in PHP

ascii-charset
Complete set of printable ASCII characters. Th...Image via Wikipedia

When dealing with multiple languages and internalization in PHP, some of the default functions in PHP end up mangling up the unicode characters in PHP. This is evident when you have a lot of funny looking characters coming up on your web page instead of the actual characters. Apart from setting the UTF-8 headers in your HTML page, you should be careful on which functions you use to handle your strings. There is an extensions called mbstring which you can install in PHP which gives you a set of functions which are unicode ( actually multibyte ) ready.

ASCII characters store each character in one byte. Unicode characters like UTF-8 use multiple bytes to handle the wider range of character sets. Some of the built in functions in PHP assume each character is only one byte and ends up breaking multibyte characters due to this assumtion.

One way to ensure that your content doesn’t get mangled up is to substitue the regular php functions in your code with the mbstring variety. To get the entire list of mbstring functions, head over to: http://php.net/manual/en/ref.mbstring.php

A few examples of the function mapping are:

EMail Function : Instead of using mail, you could use the mbstring function mb_send_mail
String Functions: strtoupper becomes mb_strtoupper; strlen becomes mb_strlen, substr becomes mb_substr and so on…

Now instead of going in and changing all your code to become multibyte ready, PHP gives you an easy way to overload the default functions with the mbstring variety.

You can set a value to mbstring.func_overload in php.ini. The value set for this function decides which functionality is overloaded by default with the mbstring variety:

  • 1 – overloads the mail functions. So you don’t have to substitute mail with mb_send_mail in your code. The mail functuion it self will work like mb_send_mail if mbstring.func_overload is set to 1 in php.ini
  • 2 – enables string functions overloading
  • 4 – enables regular expression functions overloading
  • 7 – enables mail, strings and regular expressions overloading
Zemanta Pixie

16 July 2008 View Comments

[Hack] Speed up your WordPress Delivery

[Hack] Speed up your WordPress Delivery

Here’s a how-to from AskApache.com which shows you how to improve the delivery of your WordPress blog. The article oulines a few hacks to the WP-Cache plugin to improve the cachability of WordPress. After the Wp-Cache hacks they go on to give you a few lines which you can add to your .htaccess file which

  • Set the a future expires headers which Apache sends out to the browser while serving content
  • Disabled ETag headers
  • Removes last modified Header
  • Adds cache control headers

These quick hacks ensures that the browser keeps the downloaded files ( htmls, css, images,..) and pulls them from cache next time to improve the speed at which your blog displays to the users.

Head over to the article and get started with your hacks: Hack WP-Cache for Huge Speed Increase

Zemanta Pixie

14 July 2008 View Comments

How to get Cross Browser Compatibility Every Time

How to get Cross Browser Compatibility Every Time
Lucida Grande

Anthony Short’s got a great article which shares tips on how to get your CSS cross-browser compliant everytime.

Cross-browser compatibility is one of the most time consuming tasks for any web designer. We’ve seen many different articles over the net describing common problems and fixes. I’ve collated all the information I could find to create some coding conventions for ensuring that your site will work first time in every browser. There are some things you should consider for Safari and Firefox also, and IE isn’t always the culprit for your CSS woes.

He summarizes the article by saying:

  1. Always use strict doctype and standards-compliant HTML/CSS
  2. Always use a reset at the start of your css
  3. Use -moz-opacity:0.99 on text elements to clean up rendering in Firefox, and text-shadow: #000 0 0 0 in Safari
  4. Never resize images in the CSS or HTML
  5. Check font rendering in every browser. Don’t use Lucida
  6. Size text as a % in the body, and as em’s throughout
  7. All layout divs that are floated should include display:inline and overflow:hidden
  8. Containers should have overflow:auto and trigger hasLayout via a width or height
  9. Don’t use any fancy CSS3 selectors
  10. Don’t use transparent PNG‘s unless you have loaded the alpha

Read the whole article: How to get Cross Browser Compatibility Every Time

Zemanta Pixie

18 June 2008 View Comments

MySQL Error: 1062 Duplicate entry ’0′ for key 1

MySQL

MySQL LogoHere’s a problem we had come across today. Whenever we tried inserting data into a certain table, MySQL kept throwing up the following error:

mySQL error: 1062
Duplicate entry '0' for key 1

After scratching our heads over this one, we tried to alter the table to set the autoincrement field to the next number and even that failed to fix the problem. Then hunting around the web, we found the cause of this problem. It seems that Mysql throws this error because the field type of the auto increment field is not large enough to hold the next value, so it tries to wraps the count back to ’0′ where the is already a record with that value.

We changed the field from int to unsigned bigint which fixed the problem for us. So if you face the same problem, check if your autoincrement field has maxed the datatype for that field.

Zemanta Pixie

7 June 2008 View Comments

Power your PHP Business Logic with Excel

Power your PHP Business Logic with Excel
Microsoft Excel (Windows)

I just came across this cool article which shows you how to use Excel to run the business logic of a PHP application. Maarten shows you how to use Excel to calculate the cost of a Car and get these results to show in PHP using the PHPExcel library. Under the covers, the Excel formula is parsed into the corresponding PHP code and excuted.

Here’s a scenario: You are working in a company which sells “dream cars”. For every model, the company has created an Excel spreadsheet which is used to calculate the car’s price based on customer preferences. These spreadsheets are updated frequently in order to reflect the car manufacturer’s pricing schemes.

Your manager asks you to create a small website which accepts some input fields (Does the customer want automatic transmission? What colour should the car be painted? Does the customer want leather seats? Does the customer want sports suspension?). Based on these questions, the car’s price should be calculated. Make sure all prices on the website are in sync with this Excel sheet!

Link to Maarten’s Article: Reuse Excel business logic with PHPExcel

Zemanta Pixie