php-general Digest 6 Apr 2011 12:49:33 -0000 Issue 7260
Topics (messages 312273 through 312278):
Re: randomly random
312273 by: Richard Quadling
312274 by: Jim Lucas
312275 by: Stuart Dallas
Ranges for case statement and a WTF moment.
312276 by: Richard Quadling
312277 by: Richard Quadling
Newbi Question with calculate
312278 by: Silvio Siefke
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
On 5 April 2011 15:07, Kirk Bailey <[email protected]> wrote:
> OK gang, to spew a single line from a file of fortune cookies, I want to
> read it and echo one line. While I found a 4 line code which gets it done, I
> thought there was a preexisting command to do exactly that. Any feedback on
> this?
motd
maybe.
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--- End Message ---
--- Begin Message ---
On 4/5/2011 7:07 AM, Kirk Bailey wrote:
> OK gang, to spew a single line from a file of fortune cookies, I want to read
> it
> and echo one line. While I found a 4 line code which gets it done, I thought
> there was a preexisting command to do exactly that. Any feedback on this?
>
No, but it can be done in one line:
<?php
echo array_rand(@file(@$filename), 1);
# if you wanted to do a couple checks, you could do the following
if ( is_file(@$filename) && filesize($filename) > 0 )
echo array_rand(file($filename), 1);
?>
Jim Lucas
--- End Message ---
--- Begin Message ---
On Tuesday, 5 April 2011 at 16:14, Jim Lucas wrote:
On 4/5/2011 7:07 AM, Kirk Bailey wrote:
> > OK gang, to spew a single line from a file of fortune cookies, I want to
> > read it
> > and echo one line. While I found a 4 line code which gets it done, I thought
> > there was a preexisting command to do exactly that. Any feedback on this?
>
> No, but it can be done in one line:
>
> <?php
>
> echo array_rand(@file(@$filename), 1);
>
>
> # if you wanted to do a couple checks, you could do the following
>
> if ( is_file(@$filename) && filesize($filename) > 0 )
> echo array_rand(file($filename), 1);
>
> ?>
This method will eat memory unless you have a very small file.
Personally I would use filesize to get the length of the file, fopen it, fseek
to a random position, then track back to a newline and use fgets to get the
line. Then fclose, obviously.
Simples.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--- End Message ---
--- Begin Message ---
Hi.
I just wanted to quickly see if PHP supported ranges in its
switch/case statement (http://en.wikipedia.org/wiki/Ellipsis)
<?php
$s = intval(date('s'));
switch($s)
{
case 0...9 : echo 'Between 0 and 9'; break;
case 10...19 : echo 'Between 10 and 19'; break;
case 20...29 : echo 'Between 20 and 29'; break;
case 30...39 : echo 'Between 30 and 39'; break;
case 40...49 : echo 'Between 40 and 49'; break;
case 50...59 : echo 'Between 50 and 59'; break;
default : echo 'Unknown : ', $s;
}
?>
Completely unexpectedly, the above code runs but produces the wrong output.
Interestingly, altering the number of dots and adding spaces all
result in parse errors ...
case 0..9 : // 2 dots, no spaces
case 0 .. 9 : // 2 dots, with spaces
case 0 ... 9 : // 3 dots, with spaces
It was confusing that the initial code ran without a parse error, but
considering that it did, it would suggest that the case values are
actually meaningful in some way.
php -r "var_dump(10...19);"
Interesting output ...
string(6) "100.19"
And that took me a little while to work out.
It's all to do with PHP's type juggling.
10...19
What I'm not sure is why the middle empty string is output as 0.
"10" . . ".19" becomes "10" . "0" . ".19" which becomes "100.19"
Oddly, more . don't work.
php -r "var_dump(10....19);"
all result in parse errors.
I don't know if this is a "bug" per se, but it is an oddity that I
though I'd share.
And what is even more surprising is that the initial code works in the
PHP V4.0.0. So maybe an 11 years old bug.
You really would have thought I'd have more to do with my time!
Regards,
Richard.
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--- End Message ---
--- Begin Message ---
On 5 April 2011 16:28, Richard Quadling <[email protected]> wrote:
> Hi.
>
> I just wanted to quickly see if PHP supported ranges in its
> switch/case statement (http://en.wikipedia.org/wiki/Ellipsis)
>
> <?php
> $s = intval(date('s'));
> switch($s)
> {
> case 0...9 : echo 'Between 0 and 9'; break;
> case 10...19 : echo 'Between 10 and 19'; break;
> case 20...29 : echo 'Between 20 and 29'; break;
> case 30...39 : echo 'Between 30 and 39'; break;
> case 40...49 : echo 'Between 40 and 49'; break;
> case 50...59 : echo 'Between 50 and 59'; break;
> default : echo 'Unknown : ', $s;
> }
> ?>
>
> Completely unexpectedly, the above code runs but produces the wrong output.
>
> Interestingly, altering the number of dots and adding spaces all
> result in parse errors ...
>
> case 0..9 : // 2 dots, no spaces
> case 0 .. 9 : // 2 dots, with spaces
> case 0 ... 9 : // 3 dots, with spaces
>
> It was confusing that the initial code ran without a parse error, but
> considering that it did, it would suggest that the case values are
> actually meaningful in some way.
>
> php -r "var_dump(10...19);"
>
> Interesting output ...
>
> string(6) "100.19"
>
> And that took me a little while to work out.
>
> It's all to do with PHP's type juggling.
>
> 10...19
>
> What I'm not sure is why the middle empty string is output as 0.
>
> "10" . . ".19" becomes "10" . "0" . ".19" which becomes "100.19"
>
> Oddly, more . don't work.
>
> php -r "var_dump(10....19);"
>
> all result in parse errors.
>
> I don't know if this is a "bug" per se, but it is an oddity that I
> though I'd share.
>
> And what is even more surprising is that the initial code works in the
> PHP V4.0.0. So maybe an 11 years old bug.
>
> You really would have thought I'd have more to do with my time!
>
> Regards,
>
> Richard.
Just tested PHP V3.0.11 and I get the same response.
Seems that this is just the way it is.
An oddity for since 1999-06-26 at least.
Richard.
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--- End Message ---
--- Begin Message ---
Hello,
i have write a script for my Stock portfolio and now im not really find
something which can me help.
In the table php calculate me the Present Value and the Percent with
this code:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<?php
//Quantity
$ABEI = 240;
//Buy Quote
$KKBEI = 41.31;
//Buy Balance
$KWBEI = 9914.40;
//calculate
include("../inc/quote.php"); // Yahoo Quotes
$BEIERG = $KKBEI*$ABEI; // Buy Balance
$BEIDW = $BEIQuote['last']*$ABEI; // Present Value
$BEIAEN1 = $BEIQuote['last']-$KKBEI; // Quote Win
$BEIAEN = $BEIAEN1*100/$KKBEI; //Percent Win
?>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Is it possible to apply logic to the whole table? Or do I write for each
additional item the same source code?
It were nice someone has a Tip for me? Thank u.
Silvio
--- End Message ---