Can't set cookies in php 5.3 and 5.4

I have a VPS. If I set the php version to 5.3 or 5.4 for the domain, I can’t set cookies. It gives an error:

Warning: Cannot modify header information - headers already sent by (output started at /home/…/test.php:1) in /home/…/test.php on line 2

Here is the code. There is definitely no whitespace before the opening bracket.

<?php setcookie ('test','123',0,'/'); ?>

This works fine in php 5.2.

How do I get it to work in 5.3 and 5.4?