Page 1 of 1

feedblitz counter output function

Posted: Thu Feb 17, 2011 2:41 am
by psurrena
Here's a basic function to output the subscriber count. Anything missing or unnecessary?

Code: Select all

<?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"); ?>

Re: feedblitz counter output function

Posted: Thu Feb 17, 2011 3:50 am
by Darhazer
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?

Re: feedblitz counter output function

Posted: Thu Feb 17, 2011 8:36 am
by Mordred
- short tags
- return value is a string

Re: feedblitz counter output function

Posted: Thu Feb 17, 2011 10:09 am
by psurrena
Like so? Not sure what is meant by short tags / return value is a string...

Code: Select all

<?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"); ?>

Re: feedblitz counter output function

Posted: Thu Feb 17, 2011 2:55 pm
by Mordred

Code: Select all

<?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.

Re: feedblitz counter output function

Posted: Thu Feb 17, 2011 5:07 pm
by psurrena
Great, thanks!

Re: feedblitz counter output function

Posted: Fri Feb 18, 2011 1:51 am
by Darhazer

Code: Select all

 $stats=(empty($f)) ? 'There was an error...' : $f;
return (int)$stats;
WTF? (assign a string and cast to integer)
why not

Code: Select all

function openStats($link){      
                return (int) file_get_contents($link);
        }

Re: feedblitz counter output function

Posted: Fri Feb 18, 2011 2:09 am
by Mordred
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.

Re: feedblitz counter output function

Posted: Fri Feb 18, 2011 12:34 pm
by max529
Hi,

I would write the function as below

Code: Select all

<?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