p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Function beginner


Message #1 by "Periklis Paschidis" <perikpas@o...> on Wed, 9 Oct 2002 11:28:31
Lets say that we have the following code:

<?php

$Name = "<b>MyName</b>";

function ClearHTML($clVar)
{
 	$clVar = HTMLSpecialChars($clVar);
	return;
}

echo "$Name<br>"; //This will show $Name in Bold text.

ClearHTML($Name); //Call the HTMLClear Function.

echo $Name; //This should show the $Name in normal text.

?>

I thing it's easy to understand what's the job for the function...

But! Whay dousn't it work??? Whay after the function calling is the $Name 
still shown in bold letters? (ARG!)

Help plz! :-(
Message #2 by "Pedro" <ufhbiqwj001@s...> on Wed, 9 Oct 2002 12:49:08 +0200
Hi Periklis,



your problem is that the function doesn't change the parameter.

That is by design (check

http://www.php.net/manual/en/language.references.php).



You can change that behaviour by declaring your function as taking a

reference rather than a value



<?php

function takeval($v) {

  $v = $v + 2;

  return $v;

}



function takeref(&$r) { // notice the & on the parameter list

  $r = $r + 2;

  return $r;

}



$n = 7;

echo takeval($n); // echoes 9, $n is still 7

echo $n;

echo takeref($n); // echoes 9, $n is now 9

echo $n;          // final output will be 9799

?>



also notice that when you declare a function with parameters passed by

reference you can't do



<?php

echo takeref(5); // will not work

                 // you must use a variable for this function call

?>



in php.ini there's a setting that allows you to pass by reference

at runtime, ie, define a function normally but pass a reference from

your scripts and have the function change that particular variable,

however this is deprecated and should be avoided



allow_call_time_pass_reference = Off



with this on you can do



<?php

$n = 3;

takeval(&$n); // changes $n to 5 even though takeval() takes a value

              // only works with "allow_call_time_pass_reference = On" in

php.ini



?>











I assume your code was just an example :)

.... but ... (and don't take me wrong)



.. your function ClearHTML() is just a renaming of HTMLSpecialChars()



.. it doesn't return a value (and it doesn't have any side effect either LOL)



.. it doesn't do what the name suggests





-----Original Message-----

> Lets say that we have the following code:

> 

> 

> 

> <?php

> $Name = "<b>MyName</b>";

> 

> function ClearHTML($clVar)

> {

>  	$clVar = HTMLSpecialChars($clVar);

> 	return;

> }

> 

> echo "$Name<br>"; //This will show $Name in Bold text.

> ClearHTML($Name); //Call the HTMLClear Function.

> echo $Name; //This should show the $Name in normal text.

> ?>

> 

> I thing it's easy to understand what's the job for the function...

> 

> But! Whay dousn't it work??? Whay after the function calling 

> is the $Name 

> still shown in bold letters? (ARG!)

> 

> Help plz! :-(

  Return to Index