Php使用从第一个函数到第二个函数的变量值

bogh5gae  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(121)

我有2个函数工作成功,但无法找到一种方法,使函数1中的值作为$r可以在函数2中使用(有许多其他函数,如函数2与轻微修改的计算)'所以如何看到通过函数1获取的$r添加到函数2和其他

if($NewCarVariantDetail){  
    $db = JFactory::getDBO();   
    $query = $db->getQuery(true);
    $query->select($db->qn(array('id','v_price','v_fuel_type','v_capacity')))
              ->from($db->qn('#__newprod_variants'))
               ->where([$db->qn('state') . ' = 1',
        $db->qn('id') . ' = ' . (int)$NewCarVariantDetail])
        ->order('v_price/2 asc','v_fuel_type desc');
              $db->setQuery($query);    
            $rows = $db->loadObject();
    $a = $rows->v_price;
    $b = $rows->v_fuel_type;
    $eng = $rows->v_capacity;

$z=.02034;
$p=.01809;
$q=.01779;
$qr=.01737;

$sma=7325;
$mid=11975;
$lar=27930;

if ($eng < 1000)
 {
     $r = (($z*$a)+$sma);
 }
else if((($eng >= 1000) and ($eng < 1500)))
 {
     $r = (($p*$a)+$mid);
 }
 
else if(($eng >= 1500) and ($a < 1900000))
 {
     $r = (($q*$a)+$lar);
 }

else if(($eng >= 1500) and ($a > 1900000))
 {
     $r = (($qr*$a)+$lar);
 } 
 

    
    $list='                                        
                <div class="common-box">
                    <div class="common-box-left">Basic Prem</div>
                    <div class="common-box-right">
                        <input name="voluntary_excess" type="text" class="text-box"  value="Rs. '.(round($r)) .'" readonly=""/>
                    </div>
                </div>
                ';  
    die($list);
}
  • 第二个函数,其中希望使用在第一个函数中获取的$r *
if($RtoDetail){  
    $db = JFactory::getDBO();   
    $query = $db->getQuery(true);
    $query->select($db->qn(array('id','v_price','v_fuel_type','v_capacity')))
              ->from($db->qn('#__newprod_variants'))
               ->where([$db->qn('state') . ' = 1',
                $db->qn('id') . ' = ' . (int)$RtoDetail]);
              $db->setQuery($query);    
            $rows = $db->loadObject();
    $a = $rows->v_price;
    $b = trim($rows->v_fuel_type);
    $eng = $rows->v_capacity;
    $calculatedtax = '';

if (($a < 400000) && ($b == 'Petrol'))
                {           
                    $calculatedtax = (.04*$a);
                }
                            
else if((($a >= 400000) and ($a < 600000)) and ($b == 'Petrol'))
                {           
                    $calculatedtax = (.05*$a);
                    
                }   
                
else if((($a >= 600000) and ($a < 1000000)) and ($b == 'Petrol'))
                {           
                    $calculatedtax = (.07*$a);                  
                }

else if((($a >= 1000000)) and ($b == 'Petrol'))             
                {           
                    $calculatedtax = (.10*$a);
                    
                }   
    $list='         
                <h2><u> Fees </u></h2>                
                <div class="common-box">
                    <div class="common-box-left">Total Fee </div>
                    <div class="common-box-right">
                        <input name="roadtax" type="text" class="text-box"  value="Rs. '.(round($calculatedtax)) .'" readonly=""/>
                    </div>
                </div>

                <div class="common-box">
                    <div class="common-box-left">Basic Prem</div>
                    <div class="common-box-right">
                        <input name="voluntary_excess" type="text" class="text-box"  value="Rs. '.(round($r)) .'" readonly=""/>
                    </div>
                </div>

                    
                                <div class="common-box">
                    <div class="common-box-left">Final Price </div>
                     <div class="common-box-right">
                    <input name="reg" type="text" class="text-box"  value="Rs. '.((round($a+ $calculatedtax + $r))) .'" readonly=""/>
                   
                    </div>
                </div>                                  
                ';  
    die($list);
}
zazmityj

zazmityj1#

首先,不要重复你的代码,把它变成一个函数,并在需要的地方调用它,例如myFetchFunc(...)
然后,在该函数中使用缓存技术。
但是,“如何和哪种”技术取决于您希望在多大程度上防止重复查询。
如果对每个请求重复一次查询就可以了,请使用全局变量/数组作为缓存,如下所示:

<?php

function myFetchFunc($NewCarVariantDetail) {
    // Validate arguments.
    if ( ! $NewCarVariantDetail) {
        return NULL;
    }    

    // Load from cache (if called before).
    $uniqueKey = 'myFetchResult_variant-' . $NewCarVariantDetail;
    $cache = & $GLOBALS[$uniqueKey];
    if ($cache) {
        return $cache;
    }

    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select($db->qn(array('id', 'v_price', 'v_fuel_type', 'v_capacity')))
        ->from($db->qn('#__newprod_variants'))
        ->where([$db->qn('state') . ' = 1',
            $db->qn('id') . ' = ' . (int) $NewCarVariantDetail])
        ->order('v_price/2 asc', 'v_fuel_type desc');
    $db->setQuery($query);
    $rows = $db->loadObject();
    $a = $rows->v_price;
    $b = $rows->v_fuel_type;
    $eng = $rows->v_capacity;

    $z = .02034;
    $p = .01809;
    $q = .01779;
    $qr = .01737;

    $sma = 7325;
    $mid = 11975;
    $lar = 27930;

    $r = 0; // Default value here.
    if ($eng < 1000) {
        $r = (($z * $a) + $sma);
    } else if ((($eng >= 1000) and ($eng < 1500))) {
        $r = (($p * $a) + $mid);
    } else if (($eng >= 1500) and ($a < 1900000)) {
        $r = (($q * $a) + $lar);
    } else if (($eng >= 1500) and ($a > 1900000)) {
        $r = (($qr * $a) + $lar);
    }

    // Save to cache.
    $cache = $r;
    return $r;
}

否则,有许多缓存库,它们可以存储比单个请求生存期更长的值。

相关问题