[PHP] Auto Include a Function

2003-01-09 Thread Brian T. Allen
Hi,

This may exist, but I haven't been able to find it, and I think it would
be REALLY helpful and convenient.

The idea is this:

When you write a script and call a function:



PHP would automatically look for a file named
"previously_uncalled_function" in your /include/functions/ directory.

This would eliminate a LOT of include() and require() calls (or at least
make them automatic) in a script.  The function would only get read in
if it was used.

This would be very convenient.  When you create a new function you drop
it in that directory (with a very specific, unique name, of course), and
it can immediately be called anywhere in the site.  And, you only incur
the disk IO to read it when its used for the first time in a script.

The 3 things I want to avoid are:

1)  Explicitly including every function, every time it's needed.
2)  Disk IO of including a function when it's not needed.
3)  Taking the easy route and including a file with a bunch of functions
when most won't get called.

Does this already exist, or is this a good idea (if not, any reasons
why)?  I personally would love to see it implemented if it isn't
already.

Thanks,
Brian Allen
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Fw: [PHP] unlink ($files);

2003-01-09 Thread Brian T. Allen

Hi,
You can use the backtick operator to delete whatever you have
permissions to delete:

$directory = "path/*.*";
`rm -rf $directory`;

They aren't quotes, they are backticks (to the left of the 1 key on most
keyboards).  It is very useful for any command line stuff you have
permissions to do.

Brian Allen
[EMAIL PROTECTED]


"Paul Nicholson" <[EMAIL PROTECTED]> wrote in message
news:<[EMAIL PROTECTED]>...
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
> 
> Hey,
> You can't delete the files all at onceyou have to delete them one
by one 
> or use this function that  will remove full directories.
> - -
> function force_rmdirs($dir) {
> $dh = opendir($dir);
> while(false !== ($file = readdir($dh))) {
> if($file != '.' && $file != '..') {
> $path = $dir .'/'. $file;
> if(is_dir($path)) {
> force_rmdirs($path);
> } else {
> unlink($path);
> }
> }
> }
> closedir($dh );
> return rmdir($dir);
> }
> - -
> HTH!
> ~Paul
> 
> On Wednesday 08 January 2003 12:29 am, [EMAIL PROTECTED] wrote:
> > Ok, Jason, here's another one!
> >
> > I think I've read how to delete a file.
> > I need to delete 25 files, then do a rmdir(blah);
> >
> > Line 16:  $directory = "path/*.*";
> > Warning: Unlink failed (No such file or directory) in
> > /mnt/ls6/17/169//htdocs/2003/_admin/del_listing_action.php
on line
> > 16
> >
> > Warning: RmDir failed (File exists) in
> > /mnt/ls6/17/169//htdocs/2003/_admin/del_listing_action.php
on line
> > 17
> >
> > Thanks
> 
> - -- 
> ~Paul Nicholson
> Design Specialist @ WebPower Design
> [EMAIL PROTECTED]
> www.webpowerdesign.net
> "The webthe way you want it!"
> 
> 
> "It said uses Windows 98 or better, so I loaded Linux!"
> Registered Linux User #183202 using Register Linux System # 81891
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.0.6 (GNU/Linux)
> Comment: For info see http://www.gnupg.org
> 
> iD8DBQE+G8/JDyXNIUN3+UQRAmd/AJ42CW5HDYEQ1dvf/m5CLynoqGekwgCdE5T2
> rxlRjVBaFNIhQGFQc38ylks=
> =mJq1
> -END PGP SIGNATURE-


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] restore_error_handler() to default error handler

2003-01-14 Thread Brian T. Allen
Hi,

I was playing around with error handlers, and tried this:



Which worked just fine.  A little too good, in fact.

I got over 1,200 emails in a matter of minutes (high traffic site).

I was finally able to get it to stop by re-declaring the function
without the mail() call, but how do I restore PHP's default error
handler?

I haven't had any success with restore_error_handler();

Thanks,
Brian


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: get the $email string

2003-01-16 Thread Brian T. Allen
Hi,

If you have a URL like:
http://www.whatever.com/page.php?[EMAIL PROTECTED]

Then in your script $email will already be set to "[EMAIL PROTECTED]".

This depends on register_globals being on, if I remember correctly, and
you'll want to be aware of the order for variable assignment (for POST,
GET, cookie, and session variables).

Thanks,
Brian Allen


> -Original Message-
> From: Rafael Rodrigo - NSI [mailto:[EMAIL PROTECTED]] 
> Sent: Thursday, January 16, 2003 12:03 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Re: get the $email string
> 
> 
> I don't know how do you get this answer, but try this:
> name;
> ?>
> 
> Rafael Rodrigo
> 
> > Hi,
> >
> > i made a page to display some user details and then 
> clicking on the user
> > name it goes to another page to send a mail via mail() function.
> >
> > here is the line of the first page
> >  > color="#808080"> echo $row->name;
> > ?>
> >
> > when click on user name, it goes to the 
> mail_active.php?[EMAIL PROTECTED]
> >
> > on the mail_active.php page, how do I get the e-mail 
> address to send?
> > I mean, the mail function is mail 
> ("$to","$subject","$message","From:
> > $sender");
> > How can I take the $to string to be [EMAIL PROTECTED]?
> >
> > Miguel
> >
> >
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Unsupported operand types

2003-01-18 Thread Brian T. Allen
Hi,

I've just spent the night upgrading my RedHat 6.2 server to:

Apache1.3.27
PHP4.3.0
MySQL3.23.54a
Mod_SSL2.8.12
OpenSSL0.9.7
ZendOptimizer2.1.0

Now I'm getting (inconsistently):

PHP Fatal error:  Unsupported operand types in
/web/domain/html/includes/item.inc on line 35

That line contains:
$variable1 = $variable2 - 1;

Both variables may be unassigned prior to this line, or may have numeric
values.

In a related piece of weirdness, I have this included at the bottom of
every file:

");
?>

Which normally returns something like:

-
Errors:
1062:Duplicate entry '[EMAIL PROTECTED]' for key 1

Query:


Page:   /account/new.html 
Date:   Friday January 17, 200312:42 PM

But now it returns:

Errors:
Saturday January 18, 20033:18 AM:Saturday January 18, 20033:18
AM

Query:
Array

Page:   /community/newsletter.html/2001-03-01 
Date:   Array
-

How can $date be an array, when it gets assigned right above where it
gets called?

What am I missing in this new version of PHP that is making my scripts
seemingly incompatible?

Thanks,
Brian Allen


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] session_register() Killing MySQL Connection

2003-01-21 Thread Brian T. Allen
Hi,

Friday night I upgrade my server with the following:

Apache 1.3.27
PHP 4.3.0
MySQL 3.23.54a
Mod_SSL 2.8.12
OpenSSL 0.9.7
ZendOptimizer 2.1.0

Immediately I started having really weird problems that proved almost
impossible to debug.

I finally narrowed it down to a few lines of code that get included at
the top of all the pages:



(I already know about the register_globals thing and I'm working on
getting rid of it, but that will be a long road)...

If both $REFER_SITE and $HTTP_REFERER are uninitialized ($HTTP_REFERER
may only be empty) then session_register() kills the database connection
on the page and none of my queries work...   

mysql_error() simply shows "Query was empty".

I created an otherwise empty page that had only the code above, a
database connection, and a query and confirmed that this is the case.

I bring this to the list only to help others avoid the same 3 days of
nightmarish hell I've just enjoyed on a large production site with
goblins...  And to confirm that the above is correct and seek possible
long term solutions.

Adding this line above session_register() is a temporary solution:

if(!$REFER_SITE){
  $REFER_SITE = "";
}

Thanks all!
Brian Allen
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] numbers

2003-01-21 Thread Brian T. Allen
Which, for the list, can be number_format(), among other things.

Brian

> -Original Message-
> From: cj [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, January 21, 2003 5:18 PM
> To: Php-List (E-mail)
> Subject: RE: [PHP] numbers
> 
> 
> I just found the answer
> 
> -Original Message-
> From: cj [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, 22 January 2003 11:07 AM
> To: Php-List (E-mail)
> Subject: [PHP] numbers
> 
> 
> G'day all
> 
> I have a calculation which gives me a number ie 6976587.4169922
> This number is works outed by dividing a variable with 1024
> 
> Is there any way to strip anything after the .
> so I just want the number 6976587 and not anything after the .
> 
> Thanks
> CJ
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: fscanf

2003-01-21 Thread Brian T. Allen
Given the information below this works:



I put it between pipes to confirm the absence of spaces...

It may or may not be the best way, but it is one way.

Brian

> -Original Message-
> From: Kris [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, January 21, 2003 5:02 PM
> To: Jason k Larson
> Cc: [EMAIL PROTECTED]
> Subject: Re: [PHP] Re: fscanf
> 
> 
> I've tried 
> $array = fscanf ($fp, "%s, %s, %s\n");
> earlier. It picks up
> array[0] == text,
> 
> I need 
> array[0] == text
> array[1] == text
> array[2] == text
> 
> Thanx
> 
> K
> 
> - Original Message - 
> From: "Jason k Larson" <[EMAIL PROTECTED]>
> To: "Kris" <[EMAIL PROTECTED]>
> Cc: <[EMAIL PROTECTED]>
> Sent: Wednesday, January 22, 2003 9:52 AM
> Subject: Re: [PHP] Re: fscanf
> 
> 
> > I think he is looking more for:
> > $array = fscanf ($fp, "%s, %s, %s\n");
> > 
> > But with split, wouldn't that need to be:
> > 
> > $array = split(', ', $line);
> > 
> > http://www.php.net/manual/en/function.split.php
> > or
> > 
> > $array = preg_split ('/[\s,]+/', $line);
> > using: http://www.php.net/manual/en/function.preg-split.php
> > 
> > HTH,
> > Jason k Larson
> > 
> > 
> > Philip Hallstrom wrote:
> > > Why not:
> > > 
> > > $array = split("[ ,]", $line);
> > > 
> > > ?
> > > 
> > > On Wed, 22 Jan 2003, Kris wrote:
> > > 
> > > 
> > >>Hi
> > >>
> > >>I'm having dramers with fscanf
> > >>I have a line:
> > >>text, text, text
> > >>
> > >>I can't work out how to scan removing the comma and whitespace.
> > >>the text can be absolutle anything.
> > >>
> > >>at the moment I have
> > >>$array = fscanf ($fp, %s%[^,] %s%[^,] %s%[^,]);
> > >>
> > >>Thanks
> > >>
> > >>Kris
> > 
> > 
> > 
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> > 
> > 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Re: fscanf

2003-01-22 Thread Brian T. Allen
As longs as none of the text you are using NEEDS spaces you can just do
this:



If you want to keep the spaces and there is always a space after the
comma you can do:

$string = ereg_replace(", ", ",", $string);

Instead of the other ereg_replace above.

Brian 

> -Original Message-
> From: Kris [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, January 21, 2003 6:29 PM
> To: Brian T. Allen; 'Jason k Larson'
> Cc: [EMAIL PROTECTED]
> Subject: Re: [PHP] Re: fscanf
> 
> 
> Actually found a little problem
> 
> I forgot to mention some lines can be
> 
> text, t e x t, te xt
> t e xt, text, text
> 
> This splits it into more then 3 parts.
> Any ideas on this? Shouldn't I be able to use just fscanf to 
> get the results
> I'm after??
> 
> Thanks
> 
> K
> 
> - Original Message -
> From: "Brian T. Allen" <[EMAIL PROTECTED]>
> To: "'Kris'" <[EMAIL PROTECTED]>; "'Jason k Larson'"
> <[EMAIL PROTECTED]>
> Cc: <[EMAIL PROTECTED]>
> Sent: Wednesday, January 22, 2003 10:58 AM
> Subject: RE: [PHP] Re: fscanf
> 
> 
> > Given the information below this works:
> >
> >  >
> > $string ="text1, text2, text3";
> > $array = preg_split("/[, ]/", $string, -1, PREG_SPLIT_NO_EMPTY);
> > echo "|" . $array[0] . "|" . $array[1] . "|" . $array[2] . "|";
> >
> > ?>
> >
> > I put it between pipes to confirm the absence of spaces...
> >
> > It may or may not be the best way, but it is one way.
> >
> > Brian
> >
> > > -Original Message-
> > > From: Kris [mailto:[EMAIL PROTECTED]]
> > > Sent: Tuesday, January 21, 2003 5:02 PM
> > > To: Jason k Larson
> > > Cc: [EMAIL PROTECTED]
> > > Subject: Re: [PHP] Re: fscanf
> > >
> > >
> > > I've tried
> > > $array = fscanf ($fp, "%s, %s, %s\n");
> > > earlier. It picks up
> > > array[0] == text,
> > >
> > > I need
> > > array[0] == text
> > > array[1] == text
> > > array[2] == text
> > >
> > > Thanx
> > >
> > > K
> > >
> > > - Original Message -
> > > From: "Jason k Larson" <[EMAIL PROTECTED]>
> > > To: "Kris" <[EMAIL PROTECTED]>
> > > Cc: <[EMAIL PROTECTED]>
> > > Sent: Wednesday, January 22, 2003 9:52 AM
> > > Subject: Re: [PHP] Re: fscanf
> > >
> > >
> > > > I think he is looking more for:
> > > > $array = fscanf ($fp, "%s, %s, %s\n");
> > > >
> > > > But with split, wouldn't that need to be:
> > > >
> > > > $array = split(', ', $line);
> > > >
> > > > http://www.php.net/manual/en/function.split.php
> > > > or
> > > >
> > > > $array = preg_split ('/[\s,]+/', $line);
> > > > using: http://www.php.net/manual/en/function.preg-split.php
> > > >
> > > > HTH,
> > > > Jason k Larson
> > > >
> > > >
> > > > Philip Hallstrom wrote:
> > > > > Why not:
> > > > >
> > > > > $array = split("[ ,]", $line);
> > > > >
> > > > > ?
> > > > >
> > > > > On Wed, 22 Jan 2003, Kris wrote:
> > > > >
> > > > >
> > > > >>Hi
> > > > >>
> > > > >>I'm having dramers with fscanf
> > > > >>I have a line:
> > > > >>text, text, text
> > > > >>
> > > > >>I can't work out how to scan removing the comma and 
> whitespace.
> > > > >>the text can be absolutle anything.
> > > > >>
> > > > >>at the moment I have
> > > > >>$array = fscanf ($fp, %s%[^,] %s%[^,] %s%[^,]);
> > > > >>
> > > > >>Thanks
> > > > >>
> > > > >>Kris
> > > >
> > > >
> > > >
> > > > --
> > > > PHP General Mailing List (http://www.php.net/)
> > > > To unsubscribe, visit: http://www.php.net/unsub.php
> > > >
> > > >
> > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> > >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> 
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Returning a value from a recursive function

2003-01-22 Thread Brian T. Allen
I think it will work if you return true, and just modify the global
variable (not try to output it).

Then output it (or capture it to begin with ($total_count =
OneHundred();) after the function is called.

You could also pass the variable, rather than making it global
(OneHundred("0") initially, and OneHundred($count) from within the
function). 

For instance:




Hope that helps,
Brian

> -Original Message-
> From: Andy Crain [mailto:[EMAIL PROTECTED]] 
> Sent: Wednesday, January 22, 2003 1:23 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Returning a value from a recursive function
> 
> 
> Everyone,
> 
> I'm stumped by this, even after searching the archives and the web,
> although I admit the solution likely is something very obvious.
> 
>  
> 
> I've written a function to build a string of "breadcrumb" links for a
> web site directory similar to Yahoo, etc. It queries a 
> categories table
> recursively until it reaches the root category, building a string of
> categories from the current category all the way back up to root. The
> function seems to work fine if I output directly from it 
> using echo, but
> if I instead try to return the string to the global scope and 
> then echo
> it, I get nothing. The line where I use echo/return is 
> indicated below.
> 
> Thanks,
> 
> Andy
> 
>  
> 
> function breadcrumbs($category_id=0,$mode='linked'){
> 
>global $id_array,$name_array;
> 
>  
> 
>static $counter = 0;
> 
>  
> 
>if ($category_id == 0) {
> 
>//once we're down to the root, build a return string
> 
>if ($mode == 'linked') {
> 
>   $output = 'HOME';
> 
>} else {
> 
>   $output = 'HOME';
> 
>}
> 
>while($counter > 0){
> 
>if ($mode == 'linked') {
> 
>   $output .= ' > ' . 
> array_pop($name_array)
> . '';
> 
>} else {
> 
>   $output .= ' > ' . array_pop($name_array);
> 
>}
> 
>$counter--;
> 
>}
> 
>return $output; /* "RETURN", EVERYTHING WORKS; BUT WITH "RETURN", IT DOESN'T WORK*/
> 
>} else {
> 
>$sql = 'SELECT category_id, parent_cat, category_name FROM
> web_categories WHERE category_id = ' . $category_id;
> 
>$result = safe_query($sql);
> 
>while($query_data = mysql_fetch_assoc($result)){
> 
>$id_array[$counter] = $query_data['category_id'];
> 
>$name_array[$counter] = $query_data['category_name'];
> 
>//now, move one step up and make the current category the
> previous parent
> 
>$category_id = $query_data['parent_cat'];
> 
>$counter++;
> 
>breadcrumbs($category_id,$mode);
> 
>}
> 
>}
> 
> }
> 
>  
> 
> $show = breadcrumbs(9);
> 
> echo $show;
> 
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: PHP site on CD-ROM

2001-01-20 Thread Brian T. Allen

Just create it online in such a way that you can spider your whole site and
burn it to CD.  Granted you will lose the searchability of the DB, but the
contents would all be there, and could easily be indexed so that you could
still find what you are looking for.

Create one page that has a link to all of the DB ID's, titles, descriptions,
etc.  Worse case scenario you could still use CTRL-F to find-in-page the
text you are looking for.

Granted this is clumsy compared to what you had in mind, but that is a long
trip.  You would be better off to use Delphi, VB, or something similar to
write a binary app and copy the contents of the MySQL DB to something
appropriate for your programming language and application.

$0.02.

Brian


> I wonder if it's possible to adapt the CGI version of PHP as a Netscape
> plugin, or to  associate the extension of php files to some kind of php
> wrapper.  This would require distributing a browser with the CD for this
> specific CD, but it could work.
>
> For a Unix-only kludge, I found this page,
>  http://home.netscape.com/newsref/std/x-remote.html
> It seems to me, by playing with with the application associations, one
> could get php to write to a temporary file, and then use netscape's
> -remote openFile to read it.  I imagine, though, that if this works, it
> would be rather unstable.
>
> MySQL would be a whole 'nother can of worms.
>
> Just playing with ideas.
>
> [EMAIL PROTECTED]
> http://www.artwells.com/
> That which indicates nothing
> introduces everything.
>
> On Fri, 19 Jan 2001, Philip Apostol wrote:
>
> > Can I run a PHP/Apache/MySQL services on a CD-ROM.  We have PHP scripts
that
> > handle queries on a large database.  We would like to distribute it on a
> > CD-ROM so they could access the database offline.  Is it possible? Or
are
> > there any similar solutions for this?  Im thinking of a text-file
database
> > and access it via javascript but have no much time to study on this.  If
php
> > can be run on the cd-rom, that would be a better solution.   But any
> > solution you posted here will be highly appreciated.  Thanks in advance.
> >
> > Philip
> >
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Two questions

2001-02-19 Thread Brian T. Allen

SELECT SUBSTRING(COL3,0,100) AS COL3 FROM TABLE

That will return the first 100 characters of your 10 pages of text...

If you want characters 200 through 299 do:
SELECT SUBSTRING(COL3,200,100) AS COL3 FROM TABLE

Untested, but I am 99.9% sure that will meet your needs.

Brian


>
>   hmm...I don`t need the stuff from different rows. I have 10pages of text
> in one in one rows one column, eg
> col1 col2 col3 here is 10pages
>
> S.
>
>
> > SELECT * FROM x LIMIT 1,10;
> >
> > Where  1 = Start record,
> >   10 = How many records...
> >
> > Nexts queries will be:
> >
> > SELECT * FROM x LIMIT 11,10;
> > and
> > SELECT * FROM x LIMIT 21,10;
> > etc...
> >
> >
> >  - Diogo Zulli
> >
> > --[ www.sphynx.com.br ]---
> > "Why Live on The Edge When You Can BASE Jump Off It"
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Simple math failing - PHP Bug?

2004-10-31 Thread Brian T. Allen
Hi,
OK, I'm totally stumped by this.   This should be the simplest math 
imaginable (addition and subtraction), but PHP is coming up with the 
wrong answer!

I've checked on 3 different machines (all linux) running both PHP 4 and 
PHP 5.

Here is the code to duplicate the problem:
---

$a = 2503.54;
$b = 303.55;
$c = 202.13;
$total = 0;
$total += $a;
$total += $b;
$total += $c;
$added_total = $total;
echo "$a + $b + $c = $total";
echo "Actual: $total";
$total -= $a;
$total -= $b;
$total -= $c;
echo "$added_total - $a - $b - $c = 0";
echo "Actual: $total";
?>
---
This ~should~ output this:
  2503.54 + 303.55 + 202.13 = 3009.22
  Actual: 3009.22
  3009.22 - 2503.54 - 303.55 - 202.13 = 0
  Actual: 0
But instead it outputs this:
  2503.54 + 303.55 + 202.13 = 3009.22
  Actual: 3009.22
  3009.22 - 2503.54 - 303.55 - 202.13 = 0
  Actual: 2.84217094304E-13
Now I'm the first to admit that 0.284217094304 and 0 are 
really very close, but when you are testing a variable to see if it 
equals 0, it is the difference between true and false.

This seems very, very basic to me, but I can't find the flaw in my code.
Is this legitimately bad math on the part of PHP?
Thanks,
Brian
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Simple math failing - PHP Bug?

2004-10-31 Thread Brian T. Allen
Thomas Goyne wrote:
On Sun, 31 Oct 2004 21:52:32 -0700, Brian T. Allen 
<[EMAIL PROTECTED]>  wrote:

Hi,
OK, I'm totally stumped by this.   This should be the simplest math  
imaginable (addition and subtraction), but PHP is coming up with the  
wrong answer!

[snip]
Is this legitimately bad math on the part of PHP?
Thanks,
Brian
This is a flaw in how computers handle floating point numbers, not 
php.   As the memory needed to store exact floats to any useful 
precision would  quickly get out of control, floats are handled 
inprecisely.  As a result,  you should never test for equality with a 
float, but rather check if the  float is within a certain range of the 
desired number.

Wow, this is pretty dissappointing.
I can understand if I was doing math on 12 or 13 digit numbers, but just 
adding and subtracting back out a couple of 2 digit decimals I would 
expect to end up back at zero.

I understand it isn't PHP's fault.
Thanks for the quick reply,
Brian
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Simple math failing - PHP Bug?

2004-10-31 Thread Brian T. Allen
Chris Shiflett wrote:
--- Jason Wong <[EMAIL PROTECTED]> wrote:
 

Most computer languages handling floating point calculations
just as poorly. If accuracy is important use the BCMath
functions.
   

Or use Fortran and double precision. :-)
Still, testing a floating point number as a boolean is one of the worst
ideas I've ever heard...
Chris
=
Chris Shiflett - http://shiflett.org/
PHP Security - O'Reilly HTTP Developer's Handbook - Sams
Coming January 2005 http://httphandbook.org/
 

Well, in fairness, it's one of the worst ideas you've ever heard because 
you know of this limitation.

In reality, using a calculator or something that doesn't share this 
limitation, 0 = 0 and this wouldn't be a problem.  It's only when 0 = 
0.1342344 that this becomes a problem.

Brian
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Simple math failing - PHP Bug?

2004-10-31 Thread Brian T. Allen
Chris Shiflett wrote:
--- "Brian T. Allen" <[EMAIL PROTECTED]> wrote:
 

Well, in fairness, it's one of the worst ideas you've ever heard
because you know of this limitation.
   

Of course. That wasn't a dig at you or anything - just a comment. You're
free to heed or ignore it.
 

In reality, using a calculator or something that doesn't share
this limitation, 0 = 0 and this wouldn't be a problem.
   

Well, 0 == 0 in PHP, too. Calculators have the same limitations that any
other computing device does. Some modern calculators have some pretty
impressive precision, but it's not hard to exceed it with a few seemingly
harmless operations. A couple of numbers like 12.34567 and 23.45678 can
get pretty nasty with a few operations.
Regardless of this, I'm not in favor of depending on anything that's the
least bit questionable or complicated. I don't even like evaluating
integers as boolean unless they are strictly 0 or 1, and even then there
can be problems (you accidentally decrement a variable twice before
evaluating it again, so that it goes from 1 to -1 and continues to
evaluate as true forever).
In short, complexity breeds problems, regardless of how smart you are.
Chris
=
Chris Shiflett - http://shiflett.org/
PHP Security - O'Reilly HTTP Developer's Handbook - Sams
Coming January 2005 http://httphandbook.org/
 

No worries, this isn't personal.  I just want to explore this limitation 
and it's implications for my benefit and the benefit of any that read 
this thread later when they are searching for answers to a similar problem.

I just think the following should work, and outside of this limitation 
of computers is sound programming:

$subtotal = 0;
$subtotal += 503.54;
$subtotal += 303.55;
$subtotal -= 503.54;
$subtotal -= 303.55;
if($subtotal > 0){
   // A balance is due
}
But if $subtotal is 0.00034322 then it shows a balance is due 
when one isn't.

When adding dollar amounts using only whole cents (and excluding the 
limitation we've been discussing) you should always have a credit >= 
.01, a balance >= .01, or no balance / credit due, which would equal 0 
or 0.00, but NOT 0.00034322.

Yes, there are ways to program around this, but it shouldn't be 
necessary.  The example above shouldn't be questionable or complicated, 
in fact it is about as basic as it comes.  It's 2nd Grade math.  You 
just should have to program around that.  I would MUCH rather have PHP 
run a little slower and take up more memory and get the answer right 
then have every PHP programmer have to program around this limitation 
for even the most basic math.

Thanks,
Brian
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php