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.
You cannot do this solely with PHP. You need dynamic shell scripting to create the DNS zone files for each subdomain. 

**EDIT:**

Probably @Robyflc is right, you can base conditions on the host name in PHP. It is not clear form the question if you want the subdomain or just some logic like create a URL user1.domain.com and then find the folder depending on the value of it.