Call function from included file?

I am making a PHP script that does some things for me, so I don’t have to print all the code again and again in my documents on the site.

That's what I'm doing:

// 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 seems my function is not called. Am I doing something wrong?

Here is my complete 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>
...
+3
source share
6 answers

I realized that this is because I used a variable not declared inside the function, and apparently the function needs a parameter, for example:

Function DoStuff($var)
{
  if($var != '')
  {
   return 'I am currently '.$var;
  }
}

...

echo DoStuff('posting on Stack Overflow');
+1
source
<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

+3
source

, DoStuff() ( ) . , .

+2

?

echo DoStuff();
+1

.. change

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

to

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

UPDATE

.. " "..

function GetSponsor() {

to

function GetSponsor($refid) {

HTML

<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>

-

<font style="font-size:19px" color="#0093C4" face="Calibri"><b>
This page was brought to you by: 
<?php 
include "../splashgen.php"; 
$refid = $_GET['ref']; 
echo GetSponsor($refid); ?> 
</b></font>

I also advise you to sanitize this $ refid so you don't get sql injections ...

+1
source

Have you checked the location of the inclusion? try adding "echo" hello world ", outside the function in MyFunc.php, just to make sure it is called.

0
source

All Articles