Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function from Included file?

I am making a PHP script that does some stuff for me, so I wont have to type out all the code over and over in my website documents.

Here is what I do:

// MyFunc.php
<?php
 function DoStuff()
 {
  $var = 'something'; 
  return $var;
 }
?>


// index.php
<html>
<head></head>
<body>
 Hi, I am currently doing <?php include "MyFunc.php"; echo DoStuff(); ?>, pretty cool, right?
</body>
</html>

However, it appears my function is not getting called. Am I doing anything wrong?

Here is my full source

//splashgen.php
<?php

$refid = $_GET['ref'];
$output = 'Company';

function GetSponsor()
{

    if($refid!='')
    {
        $dbhost = "localhost";
        $dbuser = "myuser";
        $dbpass = "mypass";

        $dbname = "mydb";

        $sqlselect = "SELECT * FROM egbusiness_members WHERE loginid='$refid';";

        $con = mysql_connect($dbhost,$dbuser,$dbpass) or die('Unable to connect to Database Server!');
        mysql_select_db($dbname) or die('Could Not Select Database!');

        $refid   = stripslashes($refid);
        $refid   = mysql_real_escape_string($refid);

        $result = mysql_query($sqlselect);

        while ($row = mysql_fetch_array($result))
           {
            $output = $row['name_f']." ".$row['name_l']." (".$refid.")";  
           } 
        mysql_close($con); 
    }   
    return $output;

}


?>

/////////

// index.php

...

<font style="font-size:19px" color="#0093C4" face="Calibri"><b>
This page was brought to you by: <?php $_GET['ref']; include "../splashgen.php"; echo GetSponsor(); ?> 
</b></font></div>
...
like image 998
Jeff Avatar asked May 25 '26 21:05

Jeff


2 Answers

<body>
 Hi, I am currently doing <?php include "MyFunc.php";  echo DoStuff(); ?>, pretty cool, right?
</body>

And make sure, your php files should start with <?php

like image 111
Gaurav Avatar answered May 27 '26 09:05

Gaurav


You want DoStuff() (with parentheses) to actually call the function. Apart from that your code is fine.

like image 24
Alexander Gessler Avatar answered May 27 '26 09:05

Alexander Gessler