Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2008, 08:54 AM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can PHP detect include?

Is a PHP script able to detect if it's being included in another one or being called directly? Python has a neat little trick:
Code:
if __name__ == "__main__":
that allows it to tell if the module is being run directly or if it's being imported. I want to write a bit of PHP that assigned it's output to a variable if it's being included in another, but echos the output if it's being called directly.

thanks

 
Old May 9th, 2008, 06:14 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You could just detect whether the variable you're assigning to exists.

Code:
$html = '';

include '/some/file.php';
In /some/file.php...
Code:
if (isset($html))
{
  $html .= '';
}
else
{
  echo $html;
}
Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design, 2nd Edition
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old May 11th, 2008, 06:49 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'd use output buffering in the caller to do something similar. A bit of a different approach, but keeps the controller logic out of your view.

Include and make output part of your html output:
Code:
include 'the/file.php';
Include, but assign to a variable:
Code:
ob_start();
include 'the/file.php';
$html = ob_get_contents();
ob_end_clean();

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect country using php anshul PHP How-To 5 June 11th, 2008 03:46 PM
setup database connection include file in PHP crmpicco PHP Databases 2 September 30th, 2007 04:18 PM
difference between include file & include virtual crmpicco Classic ASP Basics 2 January 23rd, 2006 11:50 AM
Virtual Include PHP file in HTML aingalsbe PHP How-To 0 May 3rd, 2004 10:37 AM
include php inside the asp file karib Classic ASP Databases 2 November 25th, 2003 03:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.