Hello everyone, I create new code for auto include files But this code not work fine (No error msg) this is simple of my code index.php <?php function new_include ($var){ include ("$var"); } new_include ( 'file.php' ); //include file.php with my function echo "$msg"; // use variable form file.php ?> PHP: file.php <?php $msg = 'Hi i\'m Khal3d'; // this is my variable i will use it in index.php file echo '<br/>this is file.php<br/>'; ?> PHP: If browse index.php , i will get "this is file.php" Only why ? why the variable not work ? plz help me! and thank you ------------------------------ I hope you understanding me because by language English is bad!
Scope. You're including the new page WITHIN the function, all variables accessed via the variable are therefore restricted to that function. function new_include ($var){ include ($var); echo $msg; } PHP: As will: function new_include ($var){ global $msg; include ($var); } PHP: Would for example work, as it has the $msg variable within the functions' scope. When you go back to global scope you can no longer use it.
Why would you want to do this in the first place? All your function does is one line: include ("$var"); infact it now takes longer to include a file.... :S