English 中文(简体)
检查在减员之前是否存在变数的最佳方式?
原标题:Best way to check if variable exists before subtraction?

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.

但是,在排第一期间,第一次必须检查一下是否已经存在补贴——总额。 我知道有选择这样做,但路不短?

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

备选案文2是:

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

现在,你认为:选择1:界定1个变量是正确的,但对于大约15个变量,我只是想到的是,是否缩短了;

希望你们理解我,我的英语并不好;-

最佳回答

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

Don t试图撰写短、快或小trick的法典。 易于阅读和维护的书写代码。

问题回答

我肯定会提倡在诉讼之前确定变数。 如果能够在预选之前实现同样的功能,则在休息室内外重复使用<条码><>发送<>>(或任何其他功能”)是浪费的。

如果你只想界定大量变数,而不必在您的休息之前明确宣布这些变量,那么你可以尝试list<><><>>docs。 或以方案方式在 lo中形成你们的变量。

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

这样,你就不必在每一行程中再次执行同样的准则(这是良好做法和良好业绩的明智之举),而且,如果你不产生我的奇克,那么,总合美元的定义是默认价值。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签