下面我有很多使用PHP创建的添加和值。
一切正常!这可以在这里看到:http://universitycompare.com
EXCEPT - 第一套包含'$ totalcost'的PHP。我似乎无法弄清楚为什么我在下面进一步创建的值不能用于创建总计。
有什么意见吗?
<?php
$totalcost = $totalcost2 + $totalcost3;
$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
?>
<!-- WORKING OUT TOTAL TCE -->
<p>
<b class="TCE">
<?php echo $totalcost; ?>
<?php echo array_sum($a); ?>
</b>
</p>
<!-- END OF WORKING OUT TOTAL TCE -->
<br /><br />
<h2>
Below is a a breakdown of the total cost of Education:
</h2>
<?php
$li2 = get_field('average_drinks_spenditure_per_night');
$li3 = ($li2 * 28) * 2.1;
$li4 = $li3 * 3;
?>
<div class="TCE">
<br />
<h2>Average spent on socialising</h2>
<ul class="TCE">
<li><span>Average drink cost</span> £<?php echo the_field('average_drink_price'); ?></li>
<li><span>Average drinks spenditure per night</span> £<?php echo $li2; ?></li>
<li><span>Average drinks spenditure for the year</span> £<?php echo $li3;?><!-- JAVA BIT --></li>
<li><span>Average drinks spenditure for 3 years</span> £<?php echo $li4;?><!-- JAVA BIT --></li>
</ul>
<?php
$li22 = (get_field('average_housing_costs_per_week') * 4 );
$li33 = $li22 * 12;
$li44 = $li33 * 3;
?>
<h2>Average spent on living costs</h2>
<ul class="TCE">
<li><span>Housing per week</span> £<?php echo the_field('average_housing_costs_per_week'); ?></li>
<li><span>Housing per month</span> £<?php echo $li22; ?></li>
<li><span>Housing per year</span> £<?php echo $li33; ?></li>
<li><span>Housing for 3 years</span> £<?php echo $li44; ?></li>
</ul>
<div class="clear"></div>
<p><br /></p>
<?php
$li222 = (get_field('average_shopping_trip_price') * 2.25 );
$li333 = $li222 * 12;
$li444 = $li333 * 3;
?>
<h2>Average spent on food shopping</h2>
<ul class="TCE">
<li><span>Average shopping trip </span> £<?php echo the_field('average_shopping_trip_price'); ?></li>
<li><span>Average shopping for the month</span> £<?php echo $li222; ?></li>
<li><span>Average shopping for the year</span> £<?php echo $li333; ?></li>
<li><span>Average shopping for 3 years</span> £<?php echo $li444; ?></li>
</ul>
<?php
$tuitionClass = (get_field('tuition_fees') /28 );
$tuitionTotal = (get_field('tuition_fees') * 3 );
?>
<h2>Average spent on tuition</h2>
<ul class="TCE">
<li><span>Price per Week</span> £<?php echo $tuitionClass; ?></li>
<li><span>Average tuition fee</span> £<?php echo the_field('tuition_fees'); ?></li>
<li><span>Average tuition fee for 3 years</span> £<?php echo $tuitionTotal; ?></li>
</ul>
答案 0 :(得分:1)
PHP脚本从上到下执行 。在定义它们的值之前,你不能只用变量计算。
答案 1 :(得分:1)
终于解决了,因为看起来我以前不知道定义总数是不可能的,有些奇怪的原因我认为它会传回来,吸取教训!
<?php
// AVERAGE DRINK COSTS
$li2 = get_field('average_drinks_spenditure_per_night');
$li3 = ($li2 * 28) * 2.1;
$li4 = $li3 * 3;
// HOUSING COSTS
$li22 = (get_field('average_housing_costs_per_week') * 4 );
$li33 = $li22 * 12;
$li44 = $li33 * 3;
// SHOPPING COSTS
$li222 = (get_field('average_shopping_trip_price') * 2.25 );
$li333 = $li222 * 12;
$li444 = $li333 * 3;
// TUITION FEES
$tuitionClass = (get_field('tuition_fees') /28 );
$tuitionTotal = (get_field('tuition_fees') * 3 );
// PHP TCE
$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
$totalcost = $totalcost2 + $totalcost3;
?>
这就是我最后使用的内容,只是将它放在回声所需的任何值之前。
答案 2 :(得分:0)
我不确定我理解你的问题。你似乎试图打印$ totalcost值之前 $ totalcost2(糟糕的名字顺便说一句!)和$ totalcost3已被计算......这些只是设置在你脚本的底部,因此会只在那时可用。如果您希望将这些值打印在页面顶部,那么您需要将所有html缓冲到一个字符串中(而不是将其转储到客户端),一旦获得了值,就将它们转储出来然后捕获页面其余部分的html字符串。那有意义吗?是否解决了你的问题?
答案 3 :(得分:0)
定义整体后,您将定义组件。
应该是这样的:
$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
$totalcost = $totalcost2 + $totalcost3;
//This line should only come after $totalcost2 and 3 were defined!
答案 4 :(得分:0)
您在有价值之前添加它们。
<?php
$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
$totalcost = $totalcost2 + $totalcost3;
?>
答案 5 :(得分:0)
据我了解,PHP解释器在程序上逐行通过PHP文件,并在解释器解析时将变量分配到内存中。由于当您尝试将它们相加时尚未定义$totalcost2
和$totalcost3
,因此PHP解释器不会引用任何值,也不能对它们求和。
如果你将这个初始代码块放在页面的底部,我想它会像你期望的那样工作。