note 61374 added to function.include

From: Date: Wed, 01 Feb 2006 22:01:46 +0000
Subject: note 61374 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-102839@lists.php.net to get a copy of this message
Re: greatmagicalhat [at] gmail [dot] com (31-Jan-2006 10:44) morris.php <A T> it-solutions.org (28-Apr-2005 05:31) Stephen Lee (19-Jan-2006 04:36) I did not grasp the importance of greatmagicalhat's statement until I ran in to troubles myself. My "gotcha" was that the PHP Variables are evaluated when the .php extension is used. My troubles got worse as I tried Stephen Lee's example which at first look worked. But if I inserted the site URL in the include it broke. Let me illustrate it with a simple example: includefile.php: <?php echo $PHP_SELF; ?> a.php <?php include 'includefile.php'; ?> Output: /a.php b.php <?php include 'http://{Yoursitename}/includefile.php'; ?> Output: /includefile.php If you in the above example change includefile.php to includefile.inc the output would be "/a.php" and "/b.php" as expected (Tested with PHP Version 4.4.1). A real world example with a typical headerfile using breadcrumbs: header.php/inc: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head><title> My Site<?php if ($title) echo ' :: '.$title; ?> </title></head><body> <?php echo '<ul>'; $parts = explode('/', dirname($PHP_SELF)); if (dirname($PHP_SELF) != '/') echo '<li><a href="/">Home ['.dirname($PHP_SELF).']</a></li>'; foreach ($parts as $key => $dir) { switch ($dir) { case 'exeception': $label = 'Something Else'; break; default: $label = ucwords($dir); break; } $url = ''; for ($i = 1; $i <= $key; $i++) { $url .= $parts[$i] . '/'; } if ($dir != '') echo '<li><a href="/'.$url.'">'.$label.'</a></li>'; } echo '</ul>'; ?> master-wrong.php: <?php include 'http://{Yoursitename}/header.php?title?My%20Title'; ?> </body></html> Output: The breadcrumbs will allways point to the location of header.php master-ok.php: <?php $title='My Title'; include 'http://{Yoursitename}/header.inc'; ?> </body></html> Output: Breadcrumbs correctly point to the location of master-ok.php ---- Server IP: 64.71.164.2 Probable Submitter: 62.243.181.185 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61374 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61374&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61374&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102839) next »