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
  • 1
  • 8

3 Answers3

3

$s instead of %s

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

or

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

Use substring function to get letters from string.

An example:

substr($s, 0, 2)
simhumileco
  • 31,877
  • 16
  • 137
  • 115
2

Second arg should be $s, see printf()

printf("[%0.2s]\n", $s);
Goma
  • 2,018
  • 1
  • 10
  • 19