-1

I am a beginner in PHP and MySQL. Here I am in stuck in one of the problem namely

Fatal error : Uncaught Error: Call to undefined function call() in PHP

in my developing application. Below is the code sample of the specific problem.

//  get the user id from the db
   $stmt = $con->prepare("SELECT * FROM doctor_specialist WHERE username = ? ");
   $stmt->bind_param("s",$doctor_name);
   $stmt->execute();
   $result = $stmt->get_result();
   if($result->num_rows > 0){
   while($row = $result->fetch_assoc()) {
   
   $user_id = $row['user_id'];

  //  check the time slots whether it is available
  $stmt9 = $con->prepare("SELECT * FROM doctor_schedule WHERE user_id = ?  AND DATE_FORMAT(end_time,'%Y-%m-%d')  = ? ");
  $stmt9->bind_param("ss",$user_id,$date);
   $stmt9->execute();
   $result9 = $stmt9->get_result();
   if($result9->num_rows > 0){
     while($row = $result9->fetch_assoc()) {

      $end_times_doctor = date('Y-m-d  H:i:s', strtotime($row['end_time']));

   }

  }

  //  check the time slots whether it is available
  $stmt10 = $con->prepare("SELECT * FROM patient_schedule WHERE user_id = ?  AND  DATE_FORMAT(end_time,'%Y-%m-%d')  = ? order by end_time DESC LIMIT 1 ");
  $stmt10->bind_param("ss",$user_id,$date);
   $stmt10->execute();
   $result10 = $stmt10->get_result();
   if($result10->num_rows > 0){ 
     while($row = $result10->fetch_assoc()) {

      $end_times_patient = date('Y-m-d  H:i:s', strtotime($row['end_time']));
      $time_interval = date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($row['end_time'])));

    }

    $diff = strtotime(date($end_times_doctor)) - strtotime($end_times_patient);
    $time = date('i:s', $diff);
   
     if ($time <= 14 ) {
   
    echo 'Time passed';
   
   
   }else{

    call();

   }
    
   } else {
    
    call();
    
function call() {
// something else

Whenever I execute the application, this will error occurred. Actually I don't know where I went wrong. Could someone help me may highly appreciated.

4
  • 2
    It looks like the call function is defined inside an else block? Commented Jul 11, 2021 at 13:22
  • @David yes. There are two call( ) functions called inside the else block. When ever becomes false, it should be call the call() function Commented Jul 11, 2021 at 13:24
  • 1
    But if the function is only defined within a given else block, wouldn't that mean it's not in scope for the other else block? Why would you define a function inside of an else block? What is the intent for doing that? Commented Jul 11, 2021 at 13:25
  • @David When the if ($time <= 14 ) { minutes is more than 15 minutes, it should call the call() function. Other thing is when there is no row in database if($result10->num_rows > 0){, it should call the call () function Commented Jul 11, 2021 at 13:27

1 Answer 1

1

Refer the documentation for conditional functions. You are currently defining a function inside an else block:

if (something) {
  if (something else) {
    //...
  } else {
    call();
  }
} else {
  call();

  function call () {
    //...
  }
}

Note the two places in which the call function is invoked. In the second place it's within the same scope that the function is defined, and PHP allows calling a function before it's defined as long as that function is in scope.

But the first place in this code where you invoke call is in an entirely different scope. In that case you're inside an if block where the function is defined in the else block. So by definition any time you try to call that function it won't be defined.

In general, don't conditionally define your functions. (Not unless you have very good reason to do so and really understand the logic you're using for it.) Define the function in a higher scope so you can call it in both blocks:

if (something) {
  if (something else) {
    //...
  } else {
    call();
  }
} else {
  call();
}

function call () {
  //...
}
Sign up to request clarification or add additional context in comments.

3 Comments

If I want to create a PHP function, should we need to create a PHP class?
@nasikthaheed: Creating a class isn't necessary in order to create a function, PHP syntax has always allowed the creation of simple global functions. Classes and OOP are relatively recent additions to PHP (they've been around a long time, but not as long as PHP itself has). Though if you would like to make your code more object-oriented then you are certainly encouraged to start with some tutorials and give it a try.
@nasikthaheed no, that isn't necessary. Functions can exist outside a class. However you seem to have missed the point of the answer. It's significant where you put the function definition (note we are talking about the definition of the function, not the lines which run the function). If you put the function definition inside an else, as you did, then it cannot be run from outside that else - this explains the error you encountered. Better to put it in the global scope of your code (if you aren't going to use classes).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.