8

I'm building an XML page inside of a function, and for some strange reason I don't get the whole thing spit out of the function. I've tried

return $thisXml;
}
echo $thisXML;

and I only get the xml declaration which is in the variable before the function. If i put an echo in the function, i get everything back as I should.

my page essentially looks like this

$thisXml = 'xml declaration stuff';

function getThisXML($thisXML){
  for(i=1; i<5; i++){
  $query "has the 5 in it";

  while ($mysqlQuery =mysql_fetch_array($theQuery) {
    $thisXml.='add the xml';
  }
  $thisXml.='close the last element';
  return $thisXml;
}

echo $thisXml;

as i said, if I replace the 'return' with 'echo', I get all the nice xml. if I echo outside the function, I only get the original declaration.

really strange, and i've been struggling with this one all day.

7 Answers 7

12
return $thisXml;
}
echo $thisXML;

$thisXML; only exists in the scope of the function. Either make $thisXML; global (bad idea) or echo getThisXML() where getThisXML is the function that returns $thisXML;

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, you are correct, I need to echo the function, not just the variable (I didn't go the global route, I was trying to avoid that). Thanks
7

Are you actually calling the function in the sense of:

$thisXml = getThisXML($someinput);

Maybe a silly question, but I don´t see it in your description.

1 Comment

$thisXml = getThisXML() was not giving the answer in my case
4

You need to invoke the function!

$thisXml = 'xml declaration stuff';

echo getThisXML($thisXML);

Or pass the variable by reference:

$thisXml = 'xml declaration stuff';

function getThisXML(&$thisXML){
  ...
  return $thisXml;
}

getThisXML($thisXML);
echo $thisXml;

1 Comment

yes, i was calling the function, forgot to put that in the code I entered above, sorry. my bad.
2

You have to call the function and apply echo on the returned value:

 $thisXml = '…';
 echo getThisXML($thisXml);

Or you pass the variably by reference.

Comments

1

You are trying to use a variable defined inside the function scope.

Use:

$thisXML;

function do(){
 global $thisXML;
 $thisXML = "foobar";
}

print $thisXML;

Comments

1

Returning a variable doesn't mean that it affects that variable globally, it means the function call evaluates to that value where it's used.

$my_var = 5;

function my_func() {
  $my_var = 10;
  return $my_var;
}

print my_func();
print "\n";
print $my_var;

This will print

10
5

Comments

0
You can create function in php this way:

<?php

$name = array("ghp", "hamid", "amin", "Linux");
function find()
{
    $find = 0;
    if(in_array('hamid', $name))
    {
      $find = 1;
      return $find;
    }
    else 
    {
      return $find;
    }
}


//###################
$answare = find();
echo $answare;
?>

1 Comment

Hi, welcome to StackOverflow! It's always better to include some explanation to the code in your answers, so that other users can understand it better :) For more info, please visit How to Ask

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.