在减法之前检查变量是否存在的最佳方法?

发布于 2024-12-27 01:47:29 字数 624 浏览 1 评论 0原文

我有一个变量,例如 $total1。它有一个来自数据库的值,例如 6。

现在我执行一个 SQL 查询,它从表中获取一些数字。例如这个: $subtraction1=5,但它处于 while 循环中,所以第二次它可能是 $subtraction1=10 或类似的东西。每次在 while 循环中,$subtraction_total 变量都是 $subtraction_total1+$subtraction1,因为在页面底部我想显示 $total 减去 $subtraction_total1。

但第一次在 while 循环中我必须检查 $subtraction_total 是否已经存在。我知道有多种选择,但是有更短的方法吗?

选项 1 是在 while 循环之前定义变量 $subtraction_total1。

选项 2 是这样做的:

(!isset($total_subtraction1)){$total_subtraction1=0;}$total_subtraction1=$total1-$subtraction1;

现在你想:好吧,只需执行选项 1:定义 1 个变量,但它适用于大约 15 个变量,我只是想知道是否有更短的方法;-)

希望你理解我,我的英语不太好;-)

I have a variable, for example $total1. It has a value from the database, for example 6.

Now I do an SQL query and it gets some numbers from tables. For example this:
$subtraction1=5, but it is in a while loop so the second time it could be $subtraction1=10 or something like that. Every time in the while loop the $subtraction_total variable would be $subtraction_total1+$subtraction1, because at the bottom of the page I would like to show the $total minus the $subtraction_total1.

But the first time in the while loop I must check if $subtraction_total already exists. I know to options to do that, but is there a shorter way?

Option 1 is to define the variable $subtraction_total1 before the while loop.

Option 2 is to do this:

(!isset($total_subtraction1)){$total_subtraction1=0;}$total_subtraction1=$total1-$subtraction1;

Now you think: well, just do option 1: define 1 variable, but it is for around 15 variables and I was just wondering if there is a shorter way;-)

Hope you understand me, my English is not very good;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当梦初醒 2025-01-03 01:47:29

在使用前定义并初始化所有变量。我认为选项 1 是符合逻辑的。

不要尝试编写短、快或棘手的代码。编写易于阅读和维护的代码。

Define and initialize all your variables before use. I think Option 1 follows logically from that.

Don't try to write code that is short, or fast or tricky. Write code that is easy to read and maintain.

山川志 2025-01-03 01:47:29

我肯定会提倡在循环之前定义变量。如果可以在循环前实现相同的功能,那么在循环内一遍又一遍地重复调用isset(或任何其他函数)是一种浪费。

如果您只是想定义大量变量,而不必在循环之前显式声明每个变量,您可以尝试 listdocs 或以编程方式在循环中创建变量。

I would definitely advocate defining the variable(s) before the loop. Repeatedly calling isset (or any other function) over and over inside a loop is wasteful if the same functionality can be achieved pre-loop.

If you're simply looking to define a large number of variables without having to explicitly declare each one before your loop you might try listdocs or programmatically create your variables in a loop.

心清如水 2025-01-03 01:47:29
$sub_total = 0;
while ($sub = mysql_get_row(...)) {
  $sub_total += $sub;
}

这样,您就不会在每次迭代中再次执行相同的代码(这是良好的实践和良好的性能),并且它还有一个额外的优点,即如果您没有来自 mysql 的结果,则 $sub_total 会使用默认值定义。

$sub_total = 0;
while ($sub = mysql_get_row(...)) {
  $sub_total += $sub;
}

This way you don't execute the same code again in every iteration (which is good practice and good performance wise), and it has the added advantage that if you have no result from mysql, $sub_total is defined with a default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文