Log in

View Full Version : Use a PHP function on an included file



jc_gmk
03-26-2008, 10:19 AM
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:


function dosomething() {
my_function_code; }

dosomething(include('mypage.php'));



The included page would look something like:


<div class="mystyle">
Some text here which will be affected by the function
</div>

codeexploiter
03-26-2008, 10:40 AM
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
//File name: file_read.php

$myFile = "test.htm";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

myPHPFunction($theData);


function myPHPFunction($data){
echo $data;
}
?>



<!-- File name: test.htm -->
<div class="mystyle">
Some text here which will be affected by the function
</div>