Author Topic: Is there a variable limit when performing many php calculations?  (Read 1094 times)

0 Members and 1 Guest are viewing this topic.

Offline fmescoTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Is there a variable limit when performing many php calculations?
« on: February 17, 2010, 04:14:43 PM »
Hi  -

I am new at php and have been learning some basics.  I now have a problem with what seems to be something simple, but I just can't figure this out and am hoping someone can shed some light on why this might be happening.

I've created an online template which is essentially an Estimate form that calculates Quotes for retail customers.  The template contains over 50 calculated fields from my database along with some variables/values that are not stored in the database.  Everything works great except that it seems that about 3 quarters of the way through the Estmate the calculations just stop calculating.

An example of one of my calculations is this:

<?php $fab_total=
$calc_shopfee +
$calc_umkit 
$calc_vanum +
$calc_disink +
$calc_cooktop +
$calc_sirail +
$calc_supports +
$calc_icorners +
$calc_radius +
$calc_arc +
$calc_outlet +
$node->field_other_fabfee[0]['value'?>
<?php $fab_total
number_format($fab_total2'.'','?>
<?php 
print check_plain ($fab_total2?>


I think this code is right, as it works in several other instances with other field names.  I should probably also mention that some of these fields are based on other calculated fields.  But the answer as to why some work and others don't has me baffled.

So, is there a limit to how many fields I can perform calculations on within php? or could it be I'm hitting a memory limit of some sort?  Are there logs that might help me troubleshoot where exactly the form is failing?  Any insight at all would be greatly appreciated.

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Is there a variable limit when performing many php calculations?
« Reply #1 on: February 17, 2010, 04:19:09 PM »
You need to terminate statements with semi-colons.

Offline fmescoTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Is there a variable limit when performing many php calculations?
« Reply #2 on: February 17, 2010, 04:56:10 PM »
Thanks roopurt18,

Could this cause my problem?  I added them in and there is still no change to the result of my equations.  They now look like this:

<?php $svc_total=
$calc_template +
$calc_install +
$calc_tearout +
$calc_rasiect +
$calc_surcharge +
$node->field_other_servicefee[0]['value']; ?>
<?php $svc_total
number_format($svc_total2'.'','); ?>
<?php 
print check_plain ($svc_total2); ?>


If only it were this simple!

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Is there a variable limit when performing many php calculations?
« Reply #3 on: February 17, 2010, 05:24:40 PM »
Try dumping each value and seeing if any are bad:
Code: [Select]
<?php 
var_dump
$calc_template$calc_install$calc_tearout$calc_rasiect$calc_surcharge$node->field_other_servicefee[0]['value'] );
$svc_total$calc_template $calc_install $calc_tearout $calc_rasiect $calc_surcharge $node->field_other_servicefee[0]['value'];
var_dump$svc_total );
$svc_totalnumber_format($svc_total2'.'',');
var_dump$svc_total );
print 
check_plain ($svc_total2);

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,815
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: Is there a variable limit when performing many php calculations?
« Reply #4 on: February 17, 2010, 05:52:49 PM »
You need to terminate statements with semi-colons.

A statement is ended either explicitly using a semi-colon or implicitly by stepping out of PHP mode using ?>, so in this case it makes no difference.

Offline fmescoTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Is there a variable limit when performing many php calculations?
« Reply #5 on: February 17, 2010, 06:06:25 PM »
roopurt18,

Thanks for that bit of code...had never done a dump before.  That's cool.  This is what it spit out:

string
(6"885.00" string(8"1,770.00" string(6"150.00" string(6"100.00" string(5"75.00" string(5"50.00" float(1261string(8"1,261.00" 1,261.00


All those numbers are correct values, except for the 1,261.00.  Those numbers should add up to 3,030.00.  This is why I'm confused...they should work, but they're not.  Any other suggestions as to what I can check to do more troubleshooting?

Thanks!

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Is there a variable limit when performing many php calculations?
« Reply #6 on: February 17, 2010, 06:26:40 PM »
The problem is that your values are strings, which means they are casted to numeric types during the addition.

In the case of "1,770.00", when you cast this to a float you get a one.

885 + 1 + 150 + 100 + 75 + 50 = 1261

Any punctuation in your values is going to throw off the automatic type-cast from string to float.

Therefore you might consider the following:
<?php
$search = array( '$', ',' ); // Add any other possible punctuation
$replace = '';
$calc_template = str_replace( $search, $replace, $calc_template );
$calc_install = str_replace( $search, $replace, $calc_install );
// repeat for each variable...

$svc_total= $calc_template + $calc_install + $calc_tearout + $calc_rasiect + $calc_surcharge + $node->field_other_servicefee[0]['value'];
$svc_total= number_format($svc_total, 2, '.', ',');
print check_plain ($svc_total, 2);
?>

Offline fmescoTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Is there a variable limit when performing many php calculations?
« Reply #7 on: February 17, 2010, 07:18:07 PM »
Wow, that worked like a charm!  Awesome!  Thanks!

Do you know if I can declare this just once with all the variables in them, or do I need to do this before each calculation group in the form?  Thanks again...I've been pulling my hair out for 2 weeks on this!

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Is there a variable limit when performing many php calculations?
« Reply #8 on: February 17, 2010, 07:32:55 PM »
You could use variable variables.  Or you can use arrays.  Whatever you pick will cut down on the final number of lines of PHP you have to write, but you'll still probably have to maintain the list of variables being filtered.

Code: [Select]
<?php 
// VARIABLE VARIABLES APPROACH
$search = array( '$'',' ); // Add any other possible punctuation
$replace '';
$tmp_other_servicefee $node->field_other_servicefee[0]['value']; // values like this go into temp variables
// just add any variables to filter to this array
$vars = array( 'calc_template''calc_install''calc_tearout''calc_rasiect'
    
'calc_surcharge''tmp_other_servicefee' );
foreach( 
$vars as $var ) {
    if( 
is_string( ${$var} ) ) {
        ${
$var} = str_replace$search$replace, ${$var} );
    }
}
// note that since you are performing addition only, you can compute $svc_total
// during the loop as i've done with the "ARRAY APPROACH" below
// if you are doing mixed operations, i.e. division, addition, multiplication, etc
// then you are best off performing the calculation in steps that clearly outline
// the order of operations.
$svc_total$calc_template $calc_install $calc_tearout $calc_rasiect $calc_surcharge $tmp_other_servicefee;
$svc_totalnumber_format($svc_total2'.'',');
print 
check_plain ($svc_total2); 
?>

Code: [Select]
<?php 
// ARRAY APPROACH
// This approach will lend itself best when the operator is the same for all of them
// (in your case: addition)
$search = array( '$'',' ); // Add any other possible punctuation
$replace '';
$values = array();
$values[] = $calc_template;
$values[] = $calc_install;
$values[] = $calc_tearout;
$values[] = $calc_rasiect;
$values[] = $calc_surcharge;
$values[] = $node->field_other_servicefee[0]['value'];
$svc_total 0;
foreach( 
$values as $key => $val ) {
    if( 
is_string$val ) ) {
        
$val str_replace$search$replace$val );
    }
    
$svc_total += $val;
}
$svc_totalnumber_format($svc_total2'.'',');
print 
check_plain ($svc_total2); 
?>

Offline Rustywolf

  • Enthusiast
  • Posts: 72
    • View Profile
Re: Is there a variable limit when performing many php calculations?
« Reply #9 on: February 22, 2010, 06:04:36 AM »
Also ,
i would suggest you use a is_int() function in there just to make sure there are no invalid characters ( or "special" characters )
« Last Edit: February 22, 2010, 06:06:03 AM by Rustywolf »

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Is there a variable limit when performing many php calculations?
« Reply #10 on: February 22, 2010, 02:51:55 PM »
is_numeric() would work better in this case.