Tag custom loop show posts

It is not recommended to use query_posts. But this is not the problem, Problem is you are using PHP variable in single quotes. 'tag=$tag'

Consider these examples:

query_posts('tag=php');
//OR
query_posts("tag=$tag"); //$tag should output: php

Using WP_query //Recommended

$query = new WP_Query(array( 'tag' => 'php' ));

Ref: WP_Query-Tag_Parameters | query_posts

error code: 523