954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

common webpage content

Hi,
I'm new to php.I had to include common content to a lot of webpages in my website like menubar etc. and i was told i could load them dynamically from a single file using php.Can someone help me with this?

AutoC
Junior Poster in Training
74 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

You can either use require_once to include another file, or if you want something more fancy, you could use templates (smarty for example).

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

As a beginner, I suggest you use the function "require" like this:

(require "includes/common.php");

Yes, require has a different syntax, to avoid future problems, always surround the hole statement with parenthesis.

kadimi
Newbie Poster
13 posts since Mar 2009
Reputation Points: 20
Solved Threads: 1
 

As a beginner, I suggest you use the function "require" like this:

(require "includes/common.php");

Yes, require has a different syntax, to avoid future problems, always surround the hole statement with parenthesis.

What future problems are you talking about ? Could you explain this a bit better ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This works fine:

if( (require "sql.php") && 1)
  echo "success";

Whereas this produces a fatal error:

if( require("sql.php") && 1)
  echo "success";


the error:Fatal error: require() [function.require]: Failed opening required '1' (include_path='.;C:\php5\pear') in D:\wamp\www\test\index.php on line 2Failed opening required '1' ... huh

-----------------------
From the PHP manual for include() (same rules apply for require):

Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.

http://www.php.net/manual/en/function.include.php

kadimi
Newbie Poster
13 posts since Mar 2009
Reputation Points: 20
Solved Threads: 1
 

I haven't seen anyone using require or include inside a brace like this though.

<?php
(include "db.php");
(require "db.php");
?>

But the point to be noted is, include and requiredo return a value on successful inclusion. That return value is OK . Strange. I didn't know that :) I wonder what would be the return value on failure.
Also, include and require are not functions. They are language constructs. Hmm! Not bad to learn few new things @ 2-30 am ? Eh ? ;)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Yes, I was wrong, these are language constructs.

I wrote "&& 1", the "1" could any arbitrary PHP expression...

kadimi
Newbie Poster
13 posts since Mar 2009
Reputation Points: 20
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You