LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-29-2008, 03:36 PM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Question PHP - How can I pass stuff to includes?


PHP newbie here. I've got a chunk of magpierss code which I've located in a ubunturss.inc include file. It looks like this:
Code:
<?php
  require_once('magpierss/rss_fetch.inc');
  $num_items = 4;
  $rss = fetch_rss( 'http://news.google.com/news?q=ubuntu+linux&ie=UTF-8&output=rss' );
  $items = array_slice($rss->items, 0, $num_items);
  foreach ($items as $item) {
    // This is a bit messy, but it makes the output valid XHTML strict by removing ampersands:
    $item['link'] = str_replace("&", "&amp;", $item['link']);
    $item['link'] = str_replace("&amp;&amp;", "&amp;", $item['link']);
    // End of messyness. Output the link and stuff:
    $href = $item['link'];
    $title = $item['title'];
    echo "\n\t\t\t\t<a href=\"$href\" target=\"_blank\">$title</a><br /><br />";
  }
echo "\n";
?>
I include this in a page by doing a:
Code:
<?php include("includes/ubunturss.inc"); ?>
But now I'd like to be able to use this include with different RSS feeds. So I would like to have a way in which when I reference the include within a .php file, I can tell it which RSS feed to use. I'm thinking there should be a way to replace the part I put in bold with some sort of variable that will take the value I give it when I do the include. Is this correct? How would I do that? Or am I thinking about this in the wrong way?

Last edited by win32sux; 02-29-2008 at 03:38 PM.
 
Old 02-29-2008, 04:38 PM   #2
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
simplist way:
ubunturss.inc
Code:
<?php
function do_rss($myurl)
{
	  require_once('magpierss/rss_fetch.inc');
	  $num_items = 4;
	  $rss = fetch_rss( $myurl );
	  $items = array_slice($rss->items, 0, $num_items);
	  foreach ($items as $item) {
		    // This is a bit messy, but it makes the output valid XHTML strict by removing ampersands:
		    $item['link'] = str_replace("&", "&amp;", $item['link']);
		    $item['link'] = str_replace("&amp;&amp;", "&amp;", $item['link']);
		    // End of messyness. Output the link and stuff:
		    $href = $item['link'];
		    $title = $item['title'];
		    echo "\n\t\t\t\t<a href=\"$href\" target=\"_blank\">$title</a><br /><br />";
	  }
	echo "\n";
}
?>
Then:
Code:
<?php include("includes/ubunturss.inc"); do_rss('http://news.google.com/news?q=ubuntu+linux&ie=UTF-8&output=rss'); ?>
 
Old 02-29-2008, 05:14 PM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Thanks for the reply, rubadub! It works fine when I use it once on a page, but when I test your suggestion with two inclusions on a page (so that I could get two different news feed blocks), the first one works fine, yet the second one fails. In the browser I get this for the second inclusion:
Quote:
Fatal error: Cannot redeclare do_rss() (previously declared in /var/www/includes/news-rss.inc:2) in /var/www/includes/news-rss.inc on line 18
I changed ubunturss.inc to news-rss.inc since it will now be generic.

The only thing on line 18 is the closing brace. Any ideas what could be causing this?


EDIT: I changed the include to include_once and it seems to work fine now (please let me know if this isn't what I should have done). Once again, thanks for the help!!!

Last edited by win32sux; 02-29-2008 at 05:31 PM.
 
Old 03-02-2008, 04:40 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
An include defines the code (in the modified case a function) now a function can only be defined once, but it can be called many times. So you add the include statement only at the beginning and then call the function as many times as you want.

There is a PHP statement include_once which ensures that the included code only gets parsed once.
 
Old 03-02-2008, 10:55 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by graemef View Post
An include defines the code (in the modified case a function) now a function can only be defined once, but it can be called many times. So you add the include statement only at the beginning and then call the function as many times as you want.

There is a PHP statement include_once which ensures that the included code only gets parsed once.
Which approach is better? include (and then call the function) or include_once?

Last edited by win32sux; 03-02-2008 at 10:56 AM.
 
Old 03-02-2008, 01:35 PM   #6
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Quote:
Which approach is better? include (and then call the function) or include_once?
require_once
 
Old 03-02-2008, 04:41 PM   #7
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by nc3b View Post
require_once
Thanks for that. Could you please elaborate a bit as to why?
 
Old 03-02-2008, 05:23 PM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
include_once will include the file only one time; if a subsequent include for that appears it will be ignored. If the file does not exist, php will warn and continue going.

require_once will include the file one time only, but requires the file to exist and php fails if the file doesn't exist.

You want to either require_once or include_once because you want your function included one time and one time only. It can be invoked as often as you need it, but can only be defined in one place.

When you tried to include your rss file twice, the first time defined the function and the second time tried to redefine the function. PHP would not permit this.
 
Old 03-02-2008, 05:38 PM   #9
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Thanks for the explanation jiml8 - much appreciated.
 
Old 03-03-2008, 03:19 PM   #10
dguitar
Member
 
Registered: Jun 2005
Location: Portland, ME
Distribution: Slackware 13, CentOS 5.3, FBSD 7.2, OBSD 4.6, Fedora 11
Posts: 122

Rep: Reputation: 17
Just fyi - your include files should have be .php not .inc (same goes for asp).

.inc is viewed as plain text in a browser
 
Old 03-03-2008, 04:26 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by dguitar View Post
.inc is viewed as plain text in a browser
I think this depends on how the server is configured.
 
Old 03-03-2008, 07:13 PM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by dguitar View Post
Just fyi - your include files should have be .php not .inc (same goes for asp).

.inc is viewed as plain text in a browser
The thinking behind the extension .inc is an aid memoir to the programmer that this file is not to be executed directly but is to be included. Whilst there is some argument that because it could be viewed as plain text this is a security risk in the world of open source that is less of an issue since the scripts can be read anyway, and are made available in a far more convenient format.

Quote:
Which approach is better? include (and then call the function) or include_once?
Just to clarify what you should be doing, in my view

First I'll just look at the difference between include and include_once. Whether you use include or include_once you should only have one include statement for each file that you which to include in the same file. To assist with that I suggest that (where possible) you promote all include statements to the top of the file. To explain:
PHP Code:
<?php
include "first.inc";
include 
"second.inc";
// your code goes here
?>
The following shows a second include statement for the same file (that is the file first.inc
PHP Code:
<?php
include "first.inc";
include 
"second.inc";
// your code goes here
include "first.inc";   // <<-- tut tut same file included a second time
// yet more code goes here
?>
But you may have seen code that does just what I have told you not to do, that is there is code that will have multiple include statements of the same file and the code works. The reason for this is that the include file will not include any php functions or classes, instead it is just php code or even just html code. In my opinion that is poor practice and it is certainly falling out of vogue (particularly since php has strengthened its support for O-O programming). Include files should really only contain functions or classes, include them once and then call the function or method.

So that is for include why include_once. That becomes useful when you have include files that need to include the other files it is them possible that you have a php script that will try and include the same file twice. So the include_once is a safe guard. Using the example above the include file first.inc, may want to use a function that is defined in second.inc and so it will include it, but once you include both in the main php script (which includes both include files) you will have problems, so in that case the include_once will bail you out.

The argument between include_once and require_once is the same and the difference between require and include has already been explained.

Sorry that this has been so long, if you managed to get this far congratulations and in short try and only use include files in your code that define functions or classes and then you code will be much easier to maintain.
 
Old 03-03-2008, 08:16 PM   #13
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Thanks for the input graemef. One question: If I would dedicate my includes to only functions and classes, where would I put my re-usable HTML blocks and stuff like that? At the moment I'm not using a MySQL server as I'm trying to get to a point where I feel reasonably comfortable with pure PHP/XHTML before going there.
 
Old 03-03-2008, 10:33 PM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I wrap them within a function.

PHP Code:
<?php
function HTML_header()
{
   
$header "";
   
$header .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\n";
   
$header .= "<head>\n";
   
$header .= "\t<title>My wonderful web page</title>\n";
   
$header .= "\t<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n";
   
$header .= "</head>\n";
   
$header .= "<body>\n";
   return 
$header;
}
?>
Then whenever you want to generate this html all you need to do is include the file it resides in call the function and echo out the string that has been returned.

Last edited by graemef; 03-03-2008 at 10:35 PM.
 
Old 03-04-2008, 08:32 AM   #15
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by win32sux View Post
Thanks for the input graemef. One question: If I would dedicate my includes to only functions and classes, where would I put my re-usable HTML blocks and stuff like that? At the moment I'm not using a MySQL server as I'm trying to get to a point where I feel reasonably comfortable with pure PHP/XHTML before going there.
Most of MY sites have a script named header.php and another script named footer.php. The exceptions are sites that I build which are fully database driven, in which I am extracting all the header information from a database entry and constructing a page with ob_start() enabled.

So, for instance, my typical page would look like this:

Code:
<?php
require_once('header.php');
(page specific code);
require_once('footer.php');
This has the effect of once again concentrating all the site specific stuff in one place; changes to header.php are instantly reflected on a site-wide basis as are changes to footer.php, and it makes the layout of the specific page quite obvious since that layout starts right after the header include.

Just another way to do the same thing.
 
  


Reply

Tags
php, rss



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
I dont understand PHP includes logicdisaster Programming 2 04-20-2005 04:00 PM
PHP includes lawadm1 Linux - Software 1 08-21-2004 04:20 PM
gcc pass 2 -no fixed includes patch problems? SciYro Linux From Scratch 0 12-22-2003 06:30 PM
php parsing some includes. others are not. toadeny Linux - Newbie 4 09-18-2003 09:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration