Jeff
Jeff

Reputation: 12163

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

Upvotes: 3

Views: 2729

Answers (6)

Jeff
Jeff

Reputation: 12163

I figured it was because I was using a variable not declared within the function, and apparently the function needs a parameter, like so:

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

...

echo DoStuff('posting on Stack Overflow');

Upvotes: 1

TigerTiger
TigerTiger

Reputation: 10806

you forgot to add parenthesis to function call.. 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

In repsonse to your update.. in your "full source" ..

Change

function GetSponsor() {

to

function GetSponsor($refid) {

and in HTML

Change

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

to something like

<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'll also advise you to sanitise this $refid so that you dont get sql injections...

Upvotes: 1

Gaurav
Gaurav

Reputation: 28755

<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

Upvotes: 3

Mild Fuzz
Mild Fuzz

Reputation: 30671

have you checked the location of the include is right? try adding "echo "hello world"; outside of the function in MyFunc.php, just to make sure it is being called.

Upvotes: 0

netbrain
netbrain

Reputation: 9304

missing paranthesis?

echo DoStuff();

Upvotes: 1

Alexander Gessler
Alexander Gessler

Reputation: 46607

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

Upvotes: 2

Related Questions