RE: [PHP] Calling a funtion inside a function

From: Date: Wed, 10 Jan 2001 18:38:25 +0000
Subject: RE: [PHP] Calling a funtion inside a function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-33727@lists.php.net to get a copy of this message
> <? > $c = "0"; > select($c, $id); > > > function select($c, $id) { > global $name, $db; > $sql = "select * from ref_directory where parent_id = '$id'"; > $results = pg_exec($db, $sql); > if (!$result) {printf ("ERROR"); exit;} > for ($pos=0;$pos<pg_numrows($result);$pos++) { > $row = pg_fetch_array($result,$pos); > $id = $row["id"]; > if ($result){ > $c++; > $name[$c] = $row[vir_dir_name]; .........................^^^^^^^^^^^^ What is that? a) a variable; then you must write $row[$vir_dir_name] b) a string; then you should write $row["vir_dir_name"] You should know, that variables from outside a function are not visible inside a function: $name = "Moritz"; function print() { echo $name; } will produce *nothing*; Instead you must write it like: $name = "Moritz"; function print() { global $name; echo $name; } (normally a function should not access a variable from outside, but sometimes there is no other way...) Hope it helps, Moritz. > select($c, $id); > } > $var = 0 > while $var < $c { > echo "<option > value=\"".$id."\">".$name[$var]."</option>\n"; > } > } > ?>

« previous php.general (#33727) next »