Results 1 to 2 of 2

Thread: Use a PHP function on an included file

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Use a PHP function on an included file

    Does anyone know if I can use a function on an included file as I can't seem to get it to work.

    This is what i'm trying:
    PHP Code:
    function dosomething() {
    my_function_code; }

    dosomething(include('mypage.php')); 
    The included page would look something like:
    HTML Code:
    <div class="mystyle">
    Some text here which will be affected by the function
    </div>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I don't think what you are trying to do is possible using PHP's include statement. It will not work in this manner.

    You can use either the file handling functions of PHP for reading the file content you wish if you want to pass the content into a PHP function or if you are dealing with JavaScript then you can try AJAX for this purpose.

    PHP example

    PHP Code:
    <?php
        
    //File name: file_read.php

        
    $myFile "test.htm";
        
    $fh fopen($myFile'r');
        
    $theData fread($fhfilesize($myFile));
        
    fclose($fh);
            
        
    myPHPFunction($theData);


        function 
    myPHPFunction($data){
                echo 
    $data;
        }
    ?>
    Code:
    <!-- File name: test.htm -->
    <div class="mystyle">
    Some text here which will be affected by the function
    </div>
    Last edited by codeexploiter; 03-26-2008 at 11:07 AM. Reason: corrections

  3. The Following User Says Thank You to codeexploiter For This Useful Post:

    jc_gmk (03-26-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •