2

I'm trying to get first 2 characters in a string. Here's my code:

$s = 'hello'; // line 4
printf("[%0.2s]\n", %s); // line 5: Gets first 2 characters [he]

It's giving me an error:

Parse error: syntax error, unexpected '%' in C:\laragon\www\karakter_tamamlama.php on line 5

Why am I getting error?

Onur Can
  • 57
  • 1
  • 7

3 Answers3

3

$s instead of %s

printf("[%0.2s]\n", $s);

or

substr($s, 0,2);
timod
  • 575
  • 3
  • 13
3

Use substring function to get letters from string.

An example:

substr($s, 0, 2)
simhumileco
  • 27,137
  • 16
  • 123
  • 105
2

Second arg should be $s, see printf()

printf("[%0.2s]\n", $s);
Goma
  • 1,988
  • 1
  • 9
  • 19