Jump to content

Is there a variable limit when performing many php calculations?


Recommended Posts

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_total, 2, '.', ',') ?>
<?php print check_plain ($fab_total, 2) ?>

 

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.

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_total, 2, '.', ','); ?>
<?php print check_plain ($svc_total, 2); ?>

 

If only it were this simple!

Try dumping each value and seeing if any are bad:

<?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_total= number_format($svc_total, 2, '.', ',');
var_dump( $svc_total );
print check_plain ($svc_total, 2);

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( "1,770.00" string(6) "150.00" string(6) "100.00" string(5) "75.00" string(5) "50.00" float(1261) string( "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!

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

?>

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!

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.

 

<?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_total= number_format($svc_total, 2, '.', ',');
print check_plain ($svc_total, 2); 
?>

 

<?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_total= number_format($svc_total, 2, '.', ',');
print check_plain ($svc_total, 2); 
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.