Jump to content

Recommended Posts

Hi everyone, I'm new to php and am in need of a little help in figuring out why my variable is not being assigned any value.

So I have a global file that has my global variable that main.php uses.  

 

global.php:

<?php

$gYearToBeginIncomePayments = 6;

// Base Contract Parameters  ( Taken from the 'Client Inputs' Tab/Sheet. ) 
$gPremiumBonusPercentage = 0.05;
$gGmvRate = 0.01;
$gCurrentCAP = 0.035;
$gCurrentParticipationRate = 0.20;
$gMinNonForefeitureRate = 0.01;
$gBenefitBasePremiumPercentage = 1.10;
$gPremium = 10000;

// Arrays
$gEndOfYear = [];
$gAgeEOY = [];
$gPremium = [];
$gBonus = [];
$gAccountValueBOY = [];
$gAnnualWithdrawalsBOY = [];
$gInterestCredited = [];
$gRiderCharge = [];
$gAccountValueEOY = [];
$gCashValueEOY = [];
$gCreditedInterestPct = [];
$gBenefitBaseBOY = [];
$gIncomeBenefit = [];
$gInterestCredited = [];
$gBenefitBaseEOY = [];
$gGuaranteedMinimumValue = [];
?>

main.php

<?php
//global variables
	$gIssueAge = $_POST['txtIssueAge'];
	$SinglePremium = $_POST['txtSinglePremiumMarket'];
	$SinglePremium = preg_replace('/[\$,]/', '', $SinglePremium);
	
$gNumberOfRows=$_POST['txtYearsToPrintMarket'];
  $LastAgeToPrint = $_POST['txtLastAgeToPrintMarket'];
	
//calculations
  for($i = 0; $i < $gNumberOfRows; $i++) $gAgeEOY [$i] = $i + $gIssueAge;
	$gPremium[0] = $SinglePremium;
 
  
  for($i = 1; $i < $gNumberOfRows; $i++) $gPremium[$i] = 0;
  $gBonus[0] = $gPremium[0] * $gPremiumBonusPercentage;
  
  $gAccountValueBOY[0] = $gPremium[0] + $gBonus[0];
  
  
  for($i = 0; $i < $gYearToBeginIncomePayments - 1; $i++) $gAnnualWithdrawalsBOY[$i] = 0;
  	$gBenefitBaseBOY[0] = ($gPremium[0] * $gBenefitBasePremiumPercentage) * $RiderSelected;
  	$gAnnualWithdrawalsBOY[0] = if($gAgeEOY < $gYearToBeginIncomePayments[0], 0, if($gAgeEOY[0] = $gYearToBeginIncomePayments[0], 0.0*$gBenefitBaseBOY[0], max($gIncomeBenefit[0],0.0*$gBenefitBaseBOY[0]) ));
  	
  for($i = 0; $i < $gNumberOfRows; $i++) {
    $gAccountValueEOY[$i] = max(0, ( $gAccountValueBOY[$i] - $gAnnualWithdrawalsBOY[$i] + $gInterestCreditedColG{$i} - $gRiderCharge{$i} ));      
    $gAccountValueBOY[$i] = $gAccountValueEOY[$i - 1];
    $gInterestCreditedColG[$i] = max(0, (( $gAccountValueBOY[$i] -  $gAnnualWithdrawalsBOY[$i] ) * $gCreditedInterestPct[$i])  );
  }
  
  for($i = 0; $i < $gNumberOfRows; $i++) {
  	$GuaranteedFirstValue = (($gPremium[0] * 0.875) * (1 + $gGmvRate)) - 50;
  	if ($i == 1) {
  	$GuaranteedMinValue = $GuaranteedFirstValue[$i];
  }
  else {
  	$gGuaranteedMinValue = max(0, ($GuaranteedFirstValue - 0) * (1 + $gPremiumBonusPercentage)) - 50;
  }
  }
?>

Issues:

 

1.  my formulas inside the loop don't show any values, such as the $Premium[0].

2. when I try to run var_dump, I don't see anything.

3. I am using dompdf to convert html to pdf by the way.

4.  all the formulas I am trying to accomplish is from a excel sheet which I am trying to calculate inside php, I don't know if this is even possible.  Perhaps this is why I don't get any values for the fields??

 

Any help would be great.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/316049-for-loop-with-if-statements/
Share on other sites

1 hour ago, pintu1228 said:
$gAnnualWithdrawalsBOY[0] = if($gAgeEOY < $gYearToBeginIncomePayments[0], 0, if($gAgeEOY[0] = $gYearToBeginIncomePayments[0], 0.0*$gBenefitBaseBOY[0], max($gIncomeBenefit[0],0.0*$gBenefitBaseBOY[0]) ));
  	

That's not even valid code.  Your code in general is poorly formatted. You should put some time into trying to format it in a way that's more readable.

 

1 hour ago, pintu1228 said:
$gPremium = 10000;

// Arrays
$gEndOfYear = [];
$gAgeEOY = [];
$gPremium = [];

You have $gPremium defined twice there as two different things.

You have other instances of trying to use a variable as both an array and a scalar value which makes no sense. For example, $gYearToBeginIncomePayments is used as an integer in a for loop, then as an array later on in your malformed if statement.

You're using {} instead of [] to access array elements in a couple places.

 

 

41 minutes ago, pintu1228 said:

Can php handle excel formulas?

PHP is not excel.  You can use PHP to accomplish the same tasks, but the syntax is different.  For example, IF is a function in excel that looks like

IF(condition, value-if-true, value-if-false)

In PHP, if is an block statement and the syntax is like this:

if (condition){
    code_if_true
} else {
    code_if_false
}

Inside each branch, you'd put whatever code you want to run, such as variable assignments.   You cannot just put a value and try to assign it as a result, like this:

$var = if (condition){
   value_if_true
} else {
   value_if_false
}

The above is invalid code and will not run.  You have to have use separate assignment statements within each branch, like this:

if (condition){
   $var = value_if_true;
} else {
   $var = value_if_false;
}

or if you have a simple conditional, you can use a ternary expression instead of a full if.

$gAnnualWithdrawalsBOY[0] = if($gAgeEOY < $gYearToBeginIncomePayments[0], 0, if($gAgeEOY[0] = $gYearToBeginIncomePayments[0], 0.0*$gBenefitBaseBOY[0], max($gIncomeBenefit[0],0.0*$gBenefitBaseBOY[0]) ));

Can you show me how to do this into a valid block statement?

Also you mentioned I am using {} instead of () to find value of array, I don't see this in my code.

 

Thanks

29 minutes ago, pintu1228 said:

Can you show me how to do this into a valid block statement?

Something like

if($gAgeEOY < $gYearToBeginIncomePayments[0]){
    $gAnnualWithdrawalsBOY[0] = 0;
} else if($gAgeEOY[0] == $gYearToBeginIncomePayments[0]){
    $gAnnualWithdrawalsBOY[0] = 0.0*$gBenefitBaseBOY[0];
} else {
    $gAnnualWithdrawalsBOY[0] = max($gIncomeBenefit[0],0.0*$gBenefitBaseBOY[0]);
}

Note, double-equals (==) for comparison, and 0.0*$anything is 0, so your first two branches are effectively the same.

29 minutes ago, pintu1228 said:

Also you mentioned I am using {} instead of () to find value of array, I don't see this in my code

instead of [].  Here:

3 hours ago, pintu1228 said:
$gInterestCreditedColG{$i} - $gRiderCharge{$i}

 

Edited by kicken

My suggestion is to STOP using multiple-case variable names.  It is going to Bite You In The youknowhere.  You are creating such long names with multiple caps in them that when you get to writing longer scripts you are going to be looking all over for stupid errors to see where you mis-typed a name.  No reason to do it, so don't do it.  That's my (strong) opinion. 

In my logs file I get this message:

 

03-27-2023 13:40:52	type: 8192		msg: __autoload() is deprecated, use spl_autoload_register() instead		file: C:\inetpub\wwwroot\include\autoload.inc.php		line: 83		context: Array
03-27-2023 13:40:52	Uncaught Exception: Error: Function name must be a string in C:\inetpub\wwwroot\market-shield-plus.inc.php:151
Stack trace:
#0 C:\inetpub\wwwroot\make-market-shield-plus\index.php(5): GetHtmlMarketShieldPlus()
#1 {main}
PHP Notice:  Array to string conversion in C:\inetpub\wwwroot\common.php on line 44

autoload.php

function __autoload($class) {
    DOMPDF_autoload($class);

 

common.php

function myHandler($type, $msg, $file, $line, $context) {
  error_log(date("m-d-Y H:i:s") . "\ttype: " . $type . "\t\tmsg: " . $msg . "\t\tfile: " . $file . "\t\tline: " . $line . "\t\tcontext: " . $context . "\r\n", 3, "c:\\illustration\\errors.log");
}

 

What does this mean and how do I fix it?

Edited by pintu1228

Hmm, what do you think about the messages it provided?

Quote

__autoload() is deprecated, use spl_autoload_register()

Manual says: https://www.php.net/manual/en/function.spl-autoload-register.php

Quote

PHP Notice: Array to string conversion in C:\inetpub\wwwroot\common.php on line 44

Assuming you identified the offending line of code as being error_log(......) one of the parameters you are passing is actually an array, and you are treating everything as a string.  If I were to guess, it's probably whatever is being passed in $context.

Again, see the php manual for the function error_log:  https://www.php.net/manual/en/function.error-log.php

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.