php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Bug with Hash Comments

Open manngo opened this issue 4 years ago • 2 comments

Description

The following code:

<?php
	$test = [
		['a','b'],
		['c','d'],
		#['e','f'],
		['g','h'],
	];
	print_r($test);

Resulted in this output:

Parse error: syntax error, unexpected single-quoted string "e" in … on line …

But I expected this output instead:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )
    [1] => Array
        (
            [0] => c
            [1] => d
        )
    [2] => Array
        (
            [0] => g
            [1] => h
        )
)

That is, the #['e','f'] line cause an error. Somehow, PHP has suddenly forgotten that # is a comment.

The version tested is 8.0.13.

Here are three things that worked:

  • Use Version 7.2.2 instead
  • Use # ['e','f'] (ie add a space after # in version 8.0.13
  • Use //['e','f'] (ie use // with or without space) in version 8.0.3

From the above I conclude that the described behaviour is unintended.

I was only able to test this on PHP 8.0.13 on Rocky Linux and PHP 7.2.2 on MacOS.

PHP Version

PHP 8.0.13

Operating System

Rocky Linux, MacOS

manngo avatar Feb 20 '22 06:02 manngo

Congratulations: you've discovered attributes! https://www.php.net/manual/en/language.attributes.php

Be sure to check the PHP 8.0 migration guide for other things you may have missed.

damianwadley avatar Feb 20 '22 06:02 damianwadley

Ah, right. I should have read the migration guide more carefully. Thanks.

manngo avatar Feb 20 '22 06:02 manngo