PHP operators 如何将变量与其他变量相乘

PHP operators How to multiply variables with other variables

我现在正在学习 PHP 并且被分配了一项我认为我可以完成的任务,但我得到了一个

'int(459)'

打印在网站上。

这是任务和我试图解决它的尝试: 用你的年龄乘以你的数字 你去上学并把它放在旁边 名为总计的变量。 然后将总数减去 3。 然后检查总计是否大于或等于 到 12 并将结果放入另一个变量中。 然后用var_dump看是真还是假。


<?php


   $age = 33;
   $schoolyears = 14;
   $total = $age * $schoolyears;

$total -= 3;

$total >= 12;
$newVar = $total;


?>

<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>

  <?php
       var_dump($newVar);
   ?>
  </body>
</html>

感谢您的回答! 罗布

更新!

编辑后它会像这样并且有效。

<?php


   $age = 33;
   $schoolyears = 14;
   $total = $age * $schoolyears;

$total -= 3;

$total = $total >= 12;
$newVar = $total;


?>

<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>

  <?php
       var_dump($newVar);
   ?>
  </body>
</html>

输出:

bool(true)

这一行什么都不做:

$total >= 12;

它产生一个值,但您不会将该值存储在任何地方。在下一行中,您只需将值从 $total(即 459)复制到一个新变量:

$newVar = $total;

您似乎打算将这些组合成这个:

$newVar = $total >= 12;

总的来说,您似乎混淆了这些运算符:

  • -=
  • >=

虽然他们共享相同的第二个角色,但他们没有任何关系。第一个从第一个值中减去第二个值并将其分配回第一个变量,一种双运算和 shorthand for:

$var1 = $var1 - $var2;

但是第二个没有赋值。它的字面意思是“大于或等于”。它执行两个值之间的比较,但不修改任何内容。

当您声明 $total >= 12; 时,它会进行检查,但不会对信息进行任何处理。

您需要设置$newVar = $total >= 12

从你的问题出发,一步步来;

 $age = 33;
 $schoolyears = 14;
 $negative = 3;
 $total = $age * $schoolyears - $negative;
 if ($total >= 12) {$result = $total; $status = true;} else {$status = false;}
 var_dump($status);

我明白了。这必须是....

$total >= 12;

像这样:

$total = $total >= 12;

谢谢,我把这个留给其他寻找类似问题的人。