It's fairly complicated. You have to use recursive functions, which are functions that actually call themselves over and over till they reach the end of the category listings.
Are you planning to use a database like MySQL or some other way to store the data? If you are, you'd probably have a scheme where each category has a categoryID and a parentID - when you call the "root" script, all categories that have a parentID of "0" are "root" categories. So your query would look like:
select * from categoryTable where parentID = 0;
Once you get deeper than that, you'd write a function that is capable of calling itself, and actually passes in (and receives) a "parentID" field. So it would be like:
listSubCategories($categoryID) {
// Here you would query for all categories with a "parentID" that's the same as $catgoryID
$q = "select * from $categoryTable where parentID = $categoryID"; // Put code after this to actually execute the query
// Then you call the listSubCategories function for each of the results
while($subCatArray = mysql_fetch_array($r)) {
listSubCategories($subCatArray[categoryID]);
}
That will recursively list through all the subcategories, and let you print them as you wish...
If this is too complex, you should probably learn some more PHP and MySQL before you attempt it 😉