Question: I really appreciate any answer to my question because I am searching for this about two weeks. My goal is to create directories using PHP and displaying them as a virtual subdomain (All procedure should have done automatically).

For example :
**example.com/test/index.php** should be considered as :
**test.example.com/index.php**

Answer: 
The only way I was able to do such thing was using a wildcard subdomain. If your server supports that, it's just a matter of using a front controller to manage the requests.
To do that, I recommand you to buy a VPS server

it's possible with URL Rewriting.