3
Is it better to declare all the variables right at the beginning of the file, even if they only come to be used, I don’t know, a thousand lines later? Or better go declaring as the program evolves?
My case is that I will unite several scripts in just one (I’m creating each function separately because it was getting too big and confusing), and I want to doubt if I should just copy and paste these files, with the variables where they are (what’s working)or if it is caught all variables and play at the beginning of this large file, and then I put only the functions.
What are the advantages and disadvantages of each of these options? Does it make a difference in performance? There is a path considered more correct?
Like, it’s better this:
// variáveis e função 1
$reds = $_POST ["Tredsalim"];
$redvalan = $_POST ["Treds4"];
$redvalan2 = $_POST ["Treds9"];
if ($reds == "sim") {
$remmes += $redvalan + $redvalan2;
}
// variáveis e função 2
$difsal = $_POST ["Tdifsalim"];
$saldev = $_POST ["Tdate5"];
$saldev2 = $_POST ["Tdate9"];
$saldev3 = $_POST ["Tdate13"];
if ($saldev > 1) {
$remmes = $saldev;
}
if ($saldev2 > 1) {
$remmes = $saldev2;
}
if ($saldev3 > 1) {
$remmes = $saldev3;
Or this:
// variáveis caso 1
$reds = $_POST ["Tredsalim"];
$redvalan = $_POST ["Treds4"];
$redvalan2 = $_POST ["Treds9"];
// variáveis caso 2
$difsal = $_POST ["Tdifsalim"];
$saldev = $_POST ["Tdate5"];
$saldev2 = $_POST ["Tdate9"];
$saldev3 = $_POST ["Tdate13"];
// função 1
if ($reds == "sim") {
$remmes += $redvalan + $redvalan2;
}
// função 2
if ($saldev > 1) {
$remmes = $saldev;
}
if ($saldev2 > 1) {
$remmes = $saldev2;
}
if ($saldev3 > 1) {
$remmes = $saldev3;
That’s what I needed to know. The scripts are well related even, and I just put in different files while I’m creating so as not to get confused. But I’m going to do just that, declare the variables first, and I’m going to add more comments too, because otherwise it’s going to be impossible to work properly. Thanks!
– gustavox