user13999717
user13999717

Reputation:

Check if a file exists and use return() to return a string variable in PHP

I have two files: I need to inclide a.php in b.php then call the a function to return a string variable and print it.

a.php:

function CheckExist($file) {
  $response;
  
  if (file_exists($file) {
    $response = "File Extisted";
  } else { 
    $response = "File Not Existed";
  }
  
  return $response;
}

b.php

// Some code to include a.php and call the function to get the string variable
print($response);

Upvotes: 0

Views: 112

Answers (1)

Clement Sam
Clement Sam

Reputation: 750

I understand you want to include file a.php in b.php and be able to use the function in a.php.

In PHP, you can include files using require_once, require, include and include_once statements which you should have probably known about before asking this question.

To suggest a solution while you read from the links I have provided, you can use any of the above functions to include your file a.php, call the function and save the return value in a variable and print it out:

b.php

require("a.php");
$response = CheckExists("a-sample-file.txt");
print($response);

Upvotes: 1

Related Questions