Variable Use Best Practices
Posted: Wed Oct 26, 2011 11:14 pm
I am a self taught php developer which probably means I don't write code according to "best practices". Recently I have been working with another PHP developer who if he wanted to write two sql queries would write the first one like so:
He would then execute the sql and write a second query like so:
Then he would execute the second query.
I would write the same code this way:
Then execute the sql and write a second query like so:
Then execute the second query.
I have two questions:
1. Is there anything wrong with re assigning the same variable name ($query) in this manner? My logic is, even though PHP has some powerful OOP capabilities it is essentially a procedural language. When I have used the variable, it's finished with so I should be able to re assign it's value later in the code.
2. Is it really necessary to concatenate each line of the query in the manner my colleague uses? Sometimes I use Navicat to write complex SQL code that I then copy and paste between applications, it's a lot easier to copy and paste between applications if it's written the way I do it.
Code: Select all
$query1 = "SELECT COUNT(fldActComp)";
$query1 .= "FROM tblcontracts WHERE fldBEAnum = '{$this->bea_number}' ";
$query1 .= "AND fldActComp IS NULL ";Code: Select all
$query2 = "SELECT COUNT(fldActComp)";
$query2 .= "FROM tblcontracts WHERE fldBEAnum = '{$this->bea_number}' ";
$query2 .= "AND fldActComp IS NOT NULL ";I would write the same code this way:
Code: Select all
$query = "SELECT COUNT(fldActComp)
FROM tblcontracts WHERE fldBEAnum = '{$this->bea_number}'
AND fldActComp IS NOT NULL ";Code: Select all
$query = "SELECT COUNT(fldActComp)
FROM tblcontracts WHERE fldBEAnum = '{$this->bea_number}'
AND fldActComp IS NULL ";I have two questions:
1. Is there anything wrong with re assigning the same variable name ($query) in this manner? My logic is, even though PHP has some powerful OOP capabilities it is essentially a procedural language. When I have used the variable, it's finished with so I should be able to re assign it's value later in the code.
2. Is it really necessary to concatenate each line of the query in the manner my colleague uses? Sometimes I use Navicat to write complex SQL code that I then copy and paste between applications, it's a lot easier to copy and paste between applications if it's written the way I do it.