1

Hy Guys,

I'm new to PHP, The PHP book I am referring it says - "Functions can be called before the actual function definition appears in code.". In the following code, I am calling _reg_form();_ before the function is defined below but I am the getting the error. What am I doing wrong.

Thanks for taking a look.

<?php
include('common_db.inc');
include('validation.php');

if(!$link=db_connect()) die(sql_error());
if($_POST['submit'])
{
  $userid = $_POST['userid'];
  $userpassword=$_POST['userpassword'];
  $username=$_POST['username'];
  $userposition = $_POST['userposition'];
  $useremail=$_POST['useremail'];
  $userprofile=$_POST['userprofile'];
  $result=validate();
  if($result==0)
  {
   reg_form();
  }
 else 
 {
   mysql_query("INSERT INTO user VALUES(NULL,'$userid',Password('$userpassword'),'$username','$userposition','$useremail','$userprofile')",$link);
 }
}
else
{
?>
<?php
function reg_form()
{
 echo "<table border='1'>
 <form action='form_test.php' method='post'>
  <tr><td>Desired ID:</td>
  <td><input type='text' size='12' name='userid' /></td></tr>
  <tr><td>Desired Password:</td>
  <td><input type='password' size='12' name='userpassword' /></td></tr> 
  <tr><td><input type='hidden' name='submit' value='true' />
  <input type='submit' value='Submit' />
  <input type='reset' value='Reset' /></td></tr>
 </form>
</table>";
}

  reg_form();
 ?>
<?php
}
?> 
1
  • why not define the function first? I'm not sure if what you are doing is possible is or not (I would guess not) but thats not really important since it would be horrible coding style in any case Commented Jan 5, 2011 at 13:16

3 Answers 3

2

You are defining a conditional function. The manual you are referring to says this (emphasis mine):

Functions need not be defined before they are referenced, except when a function is conditionally defined

Your code, shortened:

if ($something) {
     reg_form(); // use function (not defined)
} else {
     function reg_form() {
       // define function only if (!$something)
     }
}

Therefore, your function is only defined in the other branch. You'd need something like this, where the definition of your function is always executed:

if ($something) {
     reg_form(); // use function (already defined since the script was loaded)
} else {
     // something else
}

// not conditional - will be loaded when script starts
function reg_form() {
  // define function
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're defining the function within the else clause of an if() block. Most likely the function call is occuring from one of the other blocks, so the function will not have been parsed yet at the time of the call:

if (..) {
   reg_form();
} else {
   function reg_form() { ... }
}

will not work. THe function should be defined at the top level of the code, outside of any functions or logic constructs.

Comments

0

Your function reg_form is defined outside of scope in which it is called.

1 Comment

PHP doesn't have a concept of function scope like this. A conditionally defined global function still exists in the global scope, but it doesn't come into existence until the code branch containing its definition is executed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.