Emanuil Rusev
Emanuil Rusev

Reputation: 35265

Breaking an include

When you want to omit the remaining of a loop you write a break statement.

Is there something that you can write to omit the remaining of an included file (but not terminate the rest of the app like when using die or exit)?

Upvotes: 3

Views: 101

Answers (2)

Bob Baddeley
Bob Baddeley

Reputation: 2262

you could split your include into two separate files, then require the two as necessary instead of just one, or possibly just require the one and have that one require the second as necessary.

Upvotes: 1

Josh
Josh

Reputation: 11070

You can use a return statement to exit an included file, and optionally return a value.

Example:

<?php
// file1.php:

$value = include('file2.php');
echo $value;

Which includes:

<?php
// file2.php:
if($_REQUEST['something'] == 'something else')
  return 'Something else';

//do some stuff if _REQUEST['something'] != 'something else'

return 'something';

Obviously a useless example, but, it demonstrates the use of return to eit an include()ed file.

Upvotes: 7

Related Questions