PHP has a large library of built-in functions which your scripts can use. print_r() and fwrite() are just two examples. Others can be seen on php.net although it can be hard to find the one you need if you do not know their names so maybe use Google at first (but look for the results on php.net).
Go there now and look down the page at some of the functions. Then search for while using the search box. Read the description which should make some sense to you as you already used while. The examples lower down are usually most helpful.