PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 17, 2018 10:55 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Thu Feb 17, 2011 3:41 am 
Offline
Forum Contributor
User avatar

Joined: Thu Nov 10, 2005 1:31 pm
Posts: 355
Location: Broolyn, NY
Here's a basic function to output the subscriber count. Anything missing or unnecessary?

Syntax: [ Download ] [ Hide ]
<?php
        //Output Feedblitz Counter
        function openStats($link,$mode=NULL){
                $m = (empty($mode)) ? $mode : 'r';
               
                $f=file_get_contents($link,$m);
               
                $stats = (empty($f)) ? 'There was an error...' : $f;
               
                return $stats;
        }
?>


<?=openStats("http://assets.feedblitz.com/chicklets/email/i/2d/XXXXX.txt"); ?>


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 4:50 am 
Offline
DevNet Resident
User avatar

Joined: Thu May 14, 2009 3:00 pm
Posts: 1011
Location: HellCity, Bulgaria
empty will return true for 0 ;)
your function relies on allow_url_fopen setting
The mode param is wrong, as you are not using fopen, and the second argument to file_get_contents is a boolean value - bool $use_include_path
enough?


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 9:36 am 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
- short tags
- return value is a string


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 11:09 am 
Offline
Forum Contributor
User avatar

Joined: Thu Nov 10, 2005 1:31 pm
Posts: 355
Location: Broolyn, NY
Like so? Not sure what is meant by short tags / return value is a string...
Syntax: [ Download ] [ Hide ]
<?php
        //Output Feedblitz Counter
        function openStats($link){     
               
                $f=file_get_contents($link,use_include_path);
               
                $stats=(empty($f)) ? 'There was an error...' : $f;
               
                return $stats;
        }
?>

<?=openStats("http://assets.feedblitz.com/chicklets/email/i/2d/22543.txt"); ?>


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 3:55 pm 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
Syntax: [ Download ] [ Hide ]
<?php
        //Output Feedblitz Counter
        function openStats($link){      
               
                $f=file_get_contents($link,use_include_path);
               
                $stats=(empty($f)) ? 'There was an error...' : $f;
               
                return (int)$stats; //force as integer.
        }
?>

<?php echo openStats("http://assets.feedblitz.com/chicklets/email/i/2d/22543.txt"); ?>

Actually the last is a matter of style, so feel free to ignore it.


Top
 Profile  
 
PostPosted: Thu Feb 17, 2011 6:07 pm 
Offline
Forum Contributor
User avatar

Joined: Thu Nov 10, 2005 1:31 pm
Posts: 355
Location: Broolyn, NY
Great, thanks!


Top
 Profile  
 
PostPosted: Fri Feb 18, 2011 2:51 am 
Offline
DevNet Resident
User avatar

Joined: Thu May 14, 2009 3:00 pm
Posts: 1011
Location: HellCity, Bulgaria
Code:
$stats=(empty($f)) ? 'There was an error...' : $f;
return (int)$stats;

WTF? (assign a string and cast to integer)
why not
Code:
function openStats($link){     
                return (int) file_get_contents($link);
        }


Top
 Profile  
 
PostPosted: Fri Feb 18, 2011 3:09 am 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
Err well ... that's what happens when you illustrate a point without looking at the rest of the "points" around ;)
You're right of course.


Top
 Profile  
 
PostPosted: Fri Feb 18, 2011 1:34 pm 
Offline
Forum Commoner

Joined: Sat May 19, 2007 4:10 am
Posts: 50
Hi,

I would write the function as below

Syntax: [ Download ] [ Hide ]
<?php
        //Output Feedblitz Counter
        function openStats($link,$mode=NULL){
                $m = (is_null($mode)) ? $mode : 'r';
               
                $f=file_get_contents($link,$m);
               
                $stats = (strlen($f)==0) ? 'There was an error...' : $f;
               
                return $stats;
        }
?>


<?=openStats("http://assets.feedblitz.com/chicklets/email/i/2d/XXXXX.txt"); ?>


why is_null ?

improves readability. Behavior of empty function is not as apparent as behavior of is_null function. for example empty("0") return true.

why strlen?

Strictly speaking, if that file contains the string "0" your code will echo the 'There was an error...' message, because "0" is considered to be empty.
Strlen returns 0 for both the empty string and the boolean value false, the two possible return values of `file_get_contents` you would like to echo an error message.

hope this doesnt sound stupid.....

Regards,
Sandeep/Max


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group