variable in functies

Pagina: 1
Acties:

  • johu
  • Registratie: September 1999
  • Laatst online: 22-01-2024
Hoe kan ik variabele die BUITEN functies gelden ook in functies gebruiken:
(want de variablen binnen functies zijn toch 'private' ofzoiets ? )

-----------------------
<?php
$var1 = "blaat";

function test($a) {
if ($a==$var1){
return (TRUE);
}
else {
return (FALSE)
}

print (test (blaat));
?>
-----------------------

(het gaat hier dus alleen om 'hoe geef ik die variablen door' en niet een andere oplossing van 'hoe kijk ik of de variablen gelijk zijn' :) )

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 11:21

Janoz

Moderator Devschuur®

!litemod

Je kunt ze meegeven met de functie, of gewoon GLOBAL gebruiken.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • Crazy D
  • Registratie: Augustus 2000
  • Laatst online: 15-09 19:11

Crazy D

I think we should take a look.

Heel simpel: proberen.
Waarde zetten voordat je de functie ingaat.
In functie waarde afdrukken.
Als die 2 gelijk zijn, werkt het :)
Simpel as that.

Exact expert nodig?


  • johu
  • Registratie: September 1999
  • Laatst online: 22-01-2024
Op maandag 14 januari 2002 14:43 schreef Janoz het volgende:
Je kunt ze meegeven met de functie, of gewoon GLOBAL gebruiken.
Thnx "Janoz" dat was de hint die ik nodig had !

dit werkt dus niet:
-----------------------------------------
$a = 1; /* global scope */

function Test()
{
echo $a; /* reference to local scope variable */
}

Test();
-----------------------------------------

maar dit wel
-----------------------------------------
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
-----------------------------------------

omdat er nu "global" voor de variabelen staat die ik in die functie wil gebruiken :)

En dat is precies wat ik wilde :)