ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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("&", "&", $item['link']);
$item['link'] = str_replace("&&", "&", $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?
<?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("&", "&", $item['link']);
$item['link'] = str_replace("&&", "&", $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";
}
?>
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!!!
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.
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?
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.