Jump to content


PHP Include


5 replies to this topic

#1 unstopabl3

    Young Padawan

  • Members
  • Pip
  • 48 posts

Posted 31 January 2007 - 12:05 AM

Just as the topic suggests what's the best way to include a file in PHP. By best i mean speed wise and being the correct method.

I usually include files using one of the following ways

1) <? include('/home/pt/public_html/test/file.php');?>

2) <? include('../file.php');?>

3) <? include('http://www.domain.com/test/bottom.php');?>

So according to you what's the best way to include files in PHP ??? Thx in advance

#2 pirateXcore

    Young Padawan

  • Members
  • Pip
  • 281 posts
  • Gender:Male

Posted 31 January 2007 - 01:51 AM

Depends on how you need to do it.


"<? include('/home/pt/public_html/test/file.php');?>"
for some reason it doesn't always work :\ but is probably the best way

"<? include('../file.php');?>"
the way I would usually do it...but doesn't always work

"<? include('http://www.domain.com/test/bottom.php');?>"
can cause errors sometimes


The speed is (to my knowledge) the same for all of them.
Your first option is good if let's say...you're including

"everypage.php"

on
site.com/page.php
and
site.com/page/page.php

But I prefer to use what your #2 thing is.

#3 panda

    Young Padawan

  • Members
  • Pip
  • 40 posts

Posted 31 January 2007 - 02:50 AM

yea number two.
"<? include('../file.php');?>"
mainly because then you can move files around your server and reorganize your site without having to edit links although using a nav bar and links from a php include would mean you wouldnt have to :) danm im good..... please no one argue......it confuses me

#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 31 January 2007 - 11:30 AM

instead of having to type out the /home/user/bla/bla each time just use:

<?php include( $_SERVER['DOCUMENT_ROOT'].'/my_folder/file.php' ); ?>

Matt

#5 unstopabl3

    Young Padawan

  • Members
  • Pip
  • 48 posts

Posted 31 January 2007 - 07:13 PM

View PostMr. Matt, on Jan 31 2007, 04:30 PM, said:

instead of having to type out the /home/user/bla/bla each time just use:

<?php include( $_SERVER['DOCUMENT_ROOT'].'/my_folder/file.php' ); ?>

Matt

Thx alot for all the quick replies guys.

What are the differences between include and require ???
Can you use require like this

<?php require( $_SERVER['DOCUMENT_ROOT'].'/my_folder/file.php' ); ?>

Thx in advance

Edited by unstopabl3, 31 January 2007 - 07:13 PM.


#6 bay

    Young Padawan

  • Members
  • Pip
  • 105 posts
  • Gender:Male
  • Location:Chicago, IL USA

Posted 31 January 2007 - 07:39 PM

View Postunstopabl3, on Jan 31 2007, 08:13 PM, said:

What are the differences between include and require ???

basically, if you NEED the file to run the script use require, otherwise use include:

PHP.NET said:

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless.

And yes, you can include files like include( $_SERVER['DOCUMENT_ROOT'].'/my_folder/file.php' );





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users