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?

Recommended Answers

All 6 Replies

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

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.

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 ?

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 2

Failed 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

commented: :) +10

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 require do 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 ? ;)

Yes, I was wrong, these are language constructs.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.