1

How to concatenate as in the code:

<?php

print ( "h".2);

It is actually giving error

PHP Parse error:  syntax error, unexpected '.2' (T_DNUMBER) in /tmp/a.php on line 3

I've checked these Google results but unfortunately the solution does not work.

user5858
  • 1,082
  • 4
  • 39
  • 79

2 Answers2

3

PHP doesn't like it when you have no space between the . and the number (this: "h".2 - no space).

Three ways to solve this:

print ("h" . 2);

Or

print ("h"."2");

Or

$n = 2;
print ("h".$n);

In the first, simply separate the number from the string concatenation operator with a space.

In the second, wrap the number to make it a string.

In the third, use a variable to hold the number.

Martin
  • 16,093
  • 1
  • 29
  • 48
1
<?php

print( "h" . "2");

Put number into " " and have a space between dot that is used concatenation

output

h2

the problem with your solution is that you used .2 without any space. For PHP . has meaning decimal point and not joining string with number (concat) look:

print( "h" . .2);

this will give

h0.2

first dot is treated as concatenation and second .2 as decimal point that translates to 0.2

Jimmix
  • 5,644
  • 6
  • 44
  • 71