UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 5th June 2008, 01:14 PM   #1 (permalink)
bigbrennan
Registered User
 
Join Date: Jan 2006
Posts: 8
[PHP] Why is a '1' appearing after this script?

I have a simple PHP banner rotation script, the contents of banner_1 and banner_2.php:

PHP Code:
<a href="../../../../images/work/widgets.jpg" title="Blog widget and RSS up-to-the-minute feeds of the latest discussions and news from your blog or newsfeed, this application can be seeded in social media venues, downloadable from campaign websites meaning that you are delivering the news to them as it happens" class="style4" rel="lightbox">Widgets Applications</a



For some reason on the page a mysterious '1' appears from no where.


File source:

PHP Code:
              </table>
<?php
$banner_min 
1;
$banner_max 2;
$random_banner_number rand($banner_min$banner_max);
echo include(
"banner_$random_banner_number.php");?>

              <table width="200" border="0" cellspacing="0" cellpadding="10" >

Page source:


PHP Code:
</table>
<
a href="../../../../images/work/widgets.jpg" title="Blog widget and RSS up-to-the-minute feeds of the latest discussions and news from your blog or newsfeed, this application can be seeded in social media venues, downloadable from campaign websites meaning that you are delivering the news to them as it happens" class="style4" rel="lightbox">Widgets Applications</a>1     
              
<table border="0" cellpadding="10" cellspacing="0" width="200"


What is generating that random '1'?
bigbrennan is offline   Reply With Quote
Old 5th June 2008, 02:07 PM   #2 (permalink)
knapper
Senior Member
 
Join Date: Jun 2006
Location: Co. Durham
Posts: 107
The rogue 1 is after your closing anchor tag on the last bit of page source, "Widgets / Applications</a>1"
knapper is offline   Reply With Quote
Old 5th June 2008, 02:19 PM   #3 (permalink)
bigbrennan
Registered User
 
Join Date: Jan 2006
Posts: 8
Quote:
Originally Posted by knapper View Post
The rogue 1 is after your closing anchor tag on the last bit of page source, "Widgets / Applications</a>1"

Sorry, don't think I explained my post that well.

The first snippet of code is what the PHP calls upon, the 2nd snippet is the PHP code and the thrid snipper it what is output to browser.


As you see in the first snipper there is no '1' but after the PHP has ran the '1' appears.

Any ideas?
bigbrennan is offline   Reply With Quote
Old 5th June 2008, 02:30 PM   #4 (permalink)
bigbrennan
Registered User
 
Join Date: Jan 2006
Posts: 8
OK I have fixed it, not sure why but 'echo include' was messing it up. Replaced it with 'include' and it now works fine:

Before:
PHP Code:
<?php
$banner_min 
1;
$banner_max 2;
$random_banner_number rand($banner_min$banner_max);
echo include(
"banner_$random_banner_number.php");?>

After:

Before:
PHP Code:
<?php
$banner_min 
1;
$banner_max 2;
$random_banner_number rand($banner_min$banner_max);
include(
"banner_$random_banner_number.php");?>
bigbrennan is offline   Reply With Quote
Old 5th June 2008, 04:01 PM   #5 (permalink)
knapper
Senior Member
 
Join Date: Jun 2006
Location: Co. Durham
Posts: 107
Quote:
Originally Posted by bigbrennan View Post
OK I have fixed it, not sure why but 'echo include' was messing it up. Replaced it with 'include' and it now works fine:
The problem was in the initial version what you effectively had was this:

echo (so print out) include("banner_$random_banner_number.php");

What you wanted to do was not print out, but execute the function:

include("banner_$random_banner_number.php");

You dropped the echo function call and so PHP carries out the include function without interference.

Last edited by knapper : 5th June 2008 at 04:03 PM. Reason: Grammer
knapper is offline   Reply With Quote
Old 5th June 2008, 06:18 PM   #6 (permalink)
Simon
Dedicated to life!
 
Simon's Avatar
 
Join Date: Jul 2005
Location: 36°38'4.48"N - 4°42'18.52"W
Posts: 2,058
Send a message via MSN to Simon Send a message via Yahoo to Simon Send a message via Skype™ to Simon
the "include" function returns a boolean response true or false, when you echo true it is converted to a string and becomes the value 1
__________________
Freelance PHP Programming
__________________
Simon is offline   Reply With Quote
Old 5th June 2008, 06:30 PM   #7 (permalink)
Alex.
Registered User
 
Join Date: Jun 2007
Location: UK
Posts: 108
Send a message via MSN to Alex.
Hey bigbrennan,
First off include() is the wrong function to be using here - it is intended for including and evaluating php code.
The proper function for doing what you're doing is file_get_contents(), which returns the contents of the specified contents as a string.

The link was being sent to the browser because it wasn't encapsulated in php tags.

The number 1 was being sent to the browser because by default include() returns true or false depending on whether the inclusion of the file was successful.
When you echo a boolean value, it is automatically converted to either 1 (true) or 0 (false).
Alex. is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 08:01 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  UnitedForums  |  SEO by vBSEO 3.0.0
Copyright © 1998-2008 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309