There is a class file class.employee.php

<?php class employee { public function __construct($surname, $name, $patronymic, $age = 18) { $this->surname = $surname; $this->name = $name; $this->patronymic = $patronymic; $this->age = $age; } public function __toString() { return "{$this->surname} {$this->name[0]}.{$this->patronymic[0]}."; } public function __get($index) { return $this->$index; } public $surname; public $name; private $patronymic; } ?> 

have it used

 <!DOCTYPE html> <html lang="ru"> <head> <title>index</title> <meta charset="utf-8" /> </head> <body> <?php require_once("class.employee.php"); $obj = new employee("Борисов", "Игорь", "Иванович"); echo "Сотрудник $obj недавно принят на работу"; ?> </body> 

but where initials should be displayed, question marks are displayed instead of letters alt text

Both files are stored in utf-8 without BOM.
What is the problem and how to solve it?

    2 answers 2

    because of such constructions

     $this->name[0] 

    You are working with UTF8, this is a two-byte encoding. use mb_ * functions to cut a piece of string

      public function __toString() { return "{$this->surname} ".mb_substr($this->name,0,1,"utf8").".".mb_substr($this->patronymic,0,1,"utf8"); } 
    • Explain, pliz, in more detail what functions and how to use them? - Heidel
    • Completed the answer - aldem67
    • Yes, thanks, it works! - Heidel

    And if in .htaccess to register

    ADDDefaultCharset UTF-8

    • I have a local server Winginx, as I understand it, there are no other configuration files, there is no .htaccess. - Heidel