This is a discussion on how does php parse actually this? within the PHP Language forums, part of the PHP Programming Forums category; suppose i have the following code: switch ($inc): case 0: require 'inc0.php'; break; case 1: require 'inc1.php'; break; ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
suppose i have the following code:
switch ($inc): case 0: require 'inc0.php'; break; case 1: require 'inc1.php'; break; case 2: require 'inc2.php'; break; endswitch; question: does php actually include and parse all 3 files, or just the one needed (isuppose so, but i'm not sure)? how does that influence performance (each inc is about 40k)? micha |
|
|||
|
chotiwallah wrote:
> suppose i have the following code: > > switch ($inc): > case 0: > require 'inc0.php'; > break; > case 1: > require 'inc1.php'; > break; > case 2: > require 'inc2.php'; > break; > endswitch; > > question: > does php actually include and parse all 3 files, or just the one > needed (isuppose so, but i'm not sure)? how does that influence > performance (each inc is about 40k)? Only the one needed will be included in your script. Both include() and require() work pretty much the same way except require() will produce a fatal error if the file could not be included, whereas include() will just show a warning on error. I think the behaviour was changed from PHP4 on, as I'm pretty sure in PHP3 require() included the file regardless of any conditional code. -- Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com |
|
|||
|
chotiwallah wrote:
> suppose i have the following code: > > switch ($inc): > case 0: > require 'inc0.php'; > break; > case 1: > require 'inc1.php'; > break; > case 2: > require 'inc2.php'; > break; > endswitch; This doesn't exactly look like php... > question: does php actually include and parse all 3 files, or just > the one needed (isuppose so, but i'm not sure)? how does that > influence performance (each inc is about 40k)? Just read the documentation: http://nl.php.net/manual/en/function.require.php Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once. -- "He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever" |
|
|||
|
Hans van Kranenburg wrote:
> chotiwallah wrote: > >>suppose i have the following code: >> >>switch ($inc): >>case 0: >> require 'inc0.php'; >> break; >>case 1: >> require 'inc1.php'; >> break; >>case 2: >> require 'inc2.php'; >> break; >>endswitch; > > > This doesn't exactly look like php... > > >>question: does php actually include and parse all 3 files, or just >>the one needed (isuppose so, but i'm not sure)? how does that >>influence performance (each inc is about 40k)? > > > Just read the documentation: > http://nl.php.net/manual/en/function.require.php > > Note: Prior to PHP 4.0.2, the following applies: require() will always > attempt to read the target file, even if the line it's on never > executes. The conditional statement won't affect require(). However, if > the line on which the require() occurs is not executed, neither will any > of the code in the target file be executed. Similarly, looping > structures do not affect the behaviour of require(). Although the code > contained in the target file is still subject to the loop, the require() > itself happens only once. Uhm... sorry, this is not exactly the right quote... :P Anyway, including all files whether they are on a line of code that will be executed smells like asp behaviour, and the above makes clear that PHP doesn't act like that anymore since 4.0.2 -- "He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever" |
|
|||
|
On Fri, 13 May 2005 02:24:21 +0200, Hans van Kranenburg <f00b4r@xs4all.nl>
wrote: >chotiwallah wrote: >> suppose i have the following code: >> >> switch ($inc): >> case 0: >> require 'inc0.php'; >> break; >> case 1: >> require 'inc1.php'; >> break; >> case 2: >> require 'inc2.php'; >> break; >> endswitch; > >This doesn't exactly look like php... It's valid - it's the "alternative control structure syntax". This has its occasional uses for the situation when you've got a simple conditional around a chunk of literal HTML, where the literal HTML makes up the majority of your file e.g.: <?php if ($something) : ?> stuff goes here <?php endif; ?> Arguably more readable than: <?php if ($something) { ?> stuff goes here <? } ?> ... as the } in its own little bit of PHP is somewhat cryptic. Wouldn't use it in plain blocks of PHP as per the OP's post, though. -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
Andy Hassall wrote:
> On Fri, 13 May 2005 02:24:21 +0200, Hans van Kranenburg <f00b4r@xs4all.nl> > wrote: > > It's valid - it's the "alternative control structure syntax". > > This has its occasional uses for the situation when you've got a simple > conditional around a chunk of literal HTML, where the literal HTML makes up the > majority of your file e.g.: > > <?php if ($something) : ?> > > stuff goes here > > <?php endif; ?> > > > Arguably more readable than: > > > <?php if ($something) { ?> > > stuff goes here > > <? } ?> > > > ... as the } in its own little bit of PHP is somewhat cryptic. Wouldn't use it > in plain blocks of PHP as per the OP's post, though. > I ceased using this syntax a couple of years ago having been instructed that it was outdated, PHP peculiar and likely to be dropped in later versions of PHP. Was this wrong? It certainly produces cleaner scripting on occasion. Louise |
|
|||
|
On Sat, 14 May 2005 04:59:01 GMT, boclair <boclair@bigpond.net.au> wrote:
>> It's valid - it's the "alternative control structure syntax". >> >> <?php if ($something) : ?> >> >> stuff goes here >> >> <?php endif; ?> >I ceased using this syntax a couple of years ago having been instructed >that it was outdated, PHP peculiar and likely to be dropped in later >versions of PHP. Was this wrong? It certainly produces cleaner >scripting on occasion. I don't recall ever reading anything implying it'd be dropped, and it's still there in PHP5. There's no warnings to this effect in the manual that I can see. It would be incredibly foolish of the PHP maintainers to drop this since it'll simply break scripts, with no actual benefit to removing the functionality. Where did you hear this information? I've found this article: http://www.zend.com/zend/art/mistake...ew=1#Heading23 Which says: " * It is not widely used, and therefore, many people learning the language will be confused by the two syntaxes. " There are many built-in functions that are not widely used, but that shouldn't stop you using the appropriate one for the task in hand. The alternative structure syntax has a particular niche in the type of output posted earlier; it's not much good in blocks of PHP (as in the article), but it's useful in specific situations. I'd argue that: <?php } ?> ... confuses new starters more than: <?php endif; ?> ... in the "conditional around blocks of literal text" situation. " * It is not compatible with other languages, meaning that it is harder to read for people who are transitioning. " So? PHP's not compatible with Python's method of control structures either. (And what will people be transitioning from, anyway? ASP? In which case - they'll prefer endif...) " * Most importantly, someday in the future it might be disabled, forcing you to rewrite the code that implemented it. Brackets, however, will always be a part of the PHP language. " Without a reference to a documented warning that it's deprecated, this doesn't count. The article was written in 2000, five years later they're still there and still useful in a very specific situation. There are deprecated features in PHP, such as accessing characters within a string using [] instead of {} ($string[1] is deprecated but works, $string{1} is supposed to be the way to go). This is documented: http://uk2.php.net/manual/en/languag....string.substr They say it's deprecated in PHP4, so they'd have some leeway for removing it in PHP5. They haven't though. I see no similar warning for the alternative control structure syntax. I'm certainly not going to stop using it in the specific situations where it is more readable. -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
Andy Hassall wrote:
> On Sat, 14 May 2005 04:59:01 GMT, boclair <boclair@bigpond.net.au> wrote: > > >>> It's valid - it's the "alternative control structure syntax". >>> >>><?php if ($something) : ?> >>> >>>stuff goes here >>> >>><?php endif; ?> > > >>I ceased using this syntax a couple of years ago having been instructed >>that it was outdated, PHP peculiar and likely to be dropped in later >>versions of PHP. Was this wrong? It certainly produces cleaner >>scripting on occasion. > > > I don't recall ever reading anything implying it'd be dropped, and it's still > there in PHP5. There's no warnings to this effect in the manual that I can see. > > It would be incredibly foolish of the PHP maintainers to drop this since it'll > simply break scripts, with no actual benefit to removing the functionality. > > Where did you hear this information? I've found this article: > > http://www.zend.com/zend/art/mistake...ew=1#Heading23 > They were comments on one of my projects by a tutor in 2002/3. Looking back I think comments on method were marked as advisory rather than wrong. One of the problems I find with programming is discerning from reference books and other material what is true or biased or just plain wrong; (not including the Manual, of course). Thanks for your extended examination of this. Very helpful. Louise |