Need help with my function and include

Discussion in 'PHP' started by Khal3d, Feb 12, 2009.

  1. #1
    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!
     
    Khal3d, Feb 12, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    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.
     
    Danltn, Feb 12, 2009 IP
  3. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    SiteTalkZone, Feb 12, 2009 IP