This is regarding wordpress.

I've two variables $random_n and $adcount declared in every of index.php , archieve.php, search.php and single.php files at the beginning line.

The variables are declared the following:

<?php $random_n = randomNumber();?> 
<?php $adcount = 1; ?>

randomNumber() is really a function inside function.php.

Basically move individuals variables to header.php and take away them all these files, it reduces. :/ The variables can not be present in index.php, archieve.php, search.php, single.php if they're put into header.php

I have also attempted putting this code in the new file and including that file to index.php, archieve.php, search.php, and single.php, however it had not assisted too.

Key in var_dump($random_n) into the start of index.php and publish the output, please.

One possible reason is your index, search and so forth are incorporated within the function that doesn't share the scope with header.php, so that your vars are simply outdoors from the scope. It could help should you declare them global in header.php as well as your working files. But that is not recommended.

Better solution is always to introduce some Registry object (Registry pattern) and employ it.

When the variable is in some way associated with your site configuration, you need to use the wordpress option API to construct this (see here , look for option).

In case your variable is random (your function title hints that) a method to deal with this is using session variables to keep individuals values.

A great tutorial regarding how to use session variables in wordpress are available here: