Page 1 of 1

Calling function.

Posted: Sat Aug 13, 2005 1:45 am
by goodmorningsky
Hi, all
I have many years of ASP(.NET) exp, but I'm new to PHP.

Code: Select all

<?
$strDebug='Debug Inof<br>';
function debug($key, $value){
  $strDebug .= "<tr><td>$$key</td><td>$$value</td></tr>";
}
?>

<table>
<?
$unknown = 'this is testing value';

debug('$unknown', $unknown);

 echo $strDebug;
?>
</table>

This is common sense in ASP.
but, here it doesn' t show what I expected.
It seems debug function is never called.

What is problem? What I have don't wrong?
also, any comment for ASP(.NET) programmer trying to do with PHP will help.

Thank you all.

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Aug 13, 2005 2:27 am
by John Cartwright
Firstly, if you would want your code to work you would have to declare $strDebug as a global within the function. A more functional way of doing this would be to return $strDebug within the function and then assigning it onto the string itself.

Code: Select all

$strDebug='Debug Inof<br>';
function debug($key, $value){
  return "<tr><td>$key</td><td>$value</td></tr>";
}
?>

<table>
<?
$unknown = 'this is testing value';

$strDebug .= debug('$unknown', $unknown);

echo $strDebug;
?>
</table>
Take a look at the manual about functions for a more thorought explanation.
Also using a variable like $$var is considered a variable variable which bascially means the value becomes the new varname.