I need a better way to add css per each page on wordpress
Simpler approach
I haven’t tested this but based on this SO answer using global $post and $post-&gt;page_title to set $pagename should allow you to remove the loop.
function website_files_per_page() {
    global $post;
    $pagename = $post-&gt;post_title;
    if ($pagename) {
        return; //no page name
    }
    $styleName = &quot;website_&quot;.$pagename.&quot;_styles&quot;;
    $styleDir = get_theme_file_uri(&quot;/style/&quot;.$pagename.&quot;.css&quot;);
    wp_enqueue_style($styleName, $styleDir);
}
add_action(&quot;wp_print_styles&quot;, &quot;website_files_per_page&quot;);

That would allow the code to run in \$O(1)\$ time instead of \$O(n)\$.
Let PHP handle the iteration
If the suggestion to use $post doesn’t work, you may be able to change the while loop to a foreach. foreach provides an easy way to iterate over arrays:
foreach ($pagelist as $post) {
    $pagename = $post-&gt;post_name;
    if (is_page($pagename)) {
        ...

With this approach there is no need to initialize and update a counter (e.g. `$index) to dereference elements in the array.
If there was a need to access the index/counter variable the second syntax can be used for that:
foreach ($pagelist as $index =&gt; $post) {