-2

下面我有很多使用 PHP 创建的添加和值。

一切正常!它可以在这里看到:http: //universitycompare.com

除了 - 第一组 PHP 中包含“$totalcost”。我似乎无法弄清楚为什么我在下面创建的值不能用于创建总数。

有什么意见吗?

<?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>&nbsp;&nbsp;£<?php echo the_field('average_drink_price'); ?></li>
<li><span>Average drinks spenditure per night</span>&nbsp;&nbsp;£<?php echo $li2; ?></li>
<li><span>Average drinks spenditure for the year</span>&nbsp;&nbsp;£<?php echo $li3;?><!-- JAVA BIT --></li>
<li><span>Average drinks spenditure for 3 years</span>&nbsp;&nbsp;£<?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>&nbsp;&nbsp;£<?php echo the_field('average_housing_costs_per_week'); ?></li>
<li><span>Housing per month</span>&nbsp;&nbsp;£<?php echo $li22; ?></li>
<li><span>Housing per year</span>&nbsp;&nbsp;£<?php echo $li33; ?></li>
<li><span>Housing for 3 years</span>&nbsp;&nbsp;£<?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>&nbsp;&nbsp;£<?php echo the_field('average_shopping_trip_price'); ?></li>
<li><span>Average shopping for the month</span>&nbsp;&nbsp;£<?php echo $li222; ?></li>
<li><span>Average shopping for the year</span>&nbsp;&nbsp;£<?php echo $li333; ?></li>
<li><span>Average shopping for 3 years</span>&nbsp;&nbsp;£<?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>&nbsp;&nbsp;£<?php echo $tuitionClass; ?></li>
<li><span>Average tuition fee</span>&nbsp;&nbsp;£<?php echo the_field('tuition_fees'); ?></li>
<li><span>Average tuition fee for 3 years</span>&nbsp;&nbsp;£<?php echo $tuitionTotal; ?></li>
</ul>
4

6 回答 6

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

这就是我最后使用的,只是把它放在需要回显的任何值之前。

于 2012-10-16T21:15:47.950 回答
1

PHP 脚本从上到下执行。在定义变量值之前,您不能只计算变量。

于 2012-10-16T21:01:26.810 回答
0

据我了解,PHP 解释器在程序上逐行处理 PHP 文件,并在解释器解析变量时将其分配到内存中。由于您尚未定义$totalcost2并且$totalcost3当您尝试将它们相加时,PHP 解释器没有引用任何值并且无法将它们相加。

如果您将此初始代码块放在页面底部,我想它会按您的预期工作。

于 2012-10-16T21:04:45.323 回答
0

我不确定我是否理解你的问题。您似乎试图在计算 $totalcost2 (可怕的名字顺便说一句!)和 $totalcost3之前打印 $totalcost 值......这些仅设置在脚本的底部,因此仅在那时可用。如果您希望将这些值打印在页面顶部,那么您需要将所有 html 缓冲到一个字符串中(而不是将其转储到客户端),一旦您获得了您的值,将它们转储出来,然后是您捕获的页面其余部分的 html 字符串。那有意义吗?是否解决了您的问题?

于 2012-10-16T21:01:57.697 回答
0

在定义整体之后,您正在定义组件。

它应该是这样的:

$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
$totalcost = $totalcost2 + $totalcost3; 
//This line should only come after $totalcost2 and 3 were defined!
于 2012-10-16T21:02:09.490 回答
0

在它们有价值之前添加它们。

<?php
$totalcost2 = $li444 + $tuitionTotal;
$totalcost3 = $li4 + $li44;
$totalcost = $totalcost2 + $totalcost3; 
?>
于 2012-10-16T21:02:13.497 回答