I have just switched from phpdesigner to Netbeans as my script editor.
Netbeans is criticising my code with this warning
Possible accidental assignment, assignments in conditions should be avoided
What is your opinion of the warning when doing this for example? - if($data = $file($filepath))
The alternative is - $data = $file($filepath);
-
if($data)
which seems less professional.
@code green
This is discouraged in most languages and mainly because of readability, I think. TBH, I could glance over that code and think that what I saw was a comparison check. To avoid this ambiguity, I'd do it like: -
if (($data = somefunc()) != 0) { ...
-
16 9928 @code green
I don't know why it complains but.....
My organization insist me to write code in this way. -
$data = $file($filepath);
-
if($data)
-
If i club more statements in a condition like -
if($data = $file($filepath))
-
i will get review comments.
Regards
Dheeraj Joshi
I dont know about that problem, but my organization has some coding standards, as per that.
If i try to write code as -
if($data = $file($filepath))
-
my managers gives review comments to this code saying it to change as -
$data = $file($filepath);
-
if($data)
-
Regards
Dheeraj Joshi
Regards
Dheeraj Joshi
I have had a Google and it seems the reasoning behind this is to avoid the schoolboy error of confusing - if($data = $file($filepath))
and - if($data == $file($filepath))
Are script editors now assuming the lowest common denomitor of programming skill?
don’t know about that… but admittedly, if someone other than you looks at the statement - if ($var = function($param))
(s)he might not be sure, whether it is intended or a mistake.
@code green
This is discouraged in most languages and mainly because of readability, I think. TBH, I could glance over that code and think that what I saw was a comparison check. To avoid this ambiguity, I'd do it like: -
if (($data = somefunc()) != 0) { ...
-
So it seems to be discouraged for maintainability.
Hmm, I have never made the comparator/assignment error, even when reading.
But I like to comply with best practice protocol and would hate future coders criticising my legacy code.
I quite like Markus' method.
Maybe I should listen to Netbeans and modify where required
I wonder how the following code is evaluated: if ($data = somefunc())?
Is the value of somefunc() assigned to $data and then $data's value is checked, or is the assignment checked (does the assignment fail)?
I'm going to ask on the PHP dev mailing list.
I don’t think an assignment can fail… it’s usually expressions that fail.
I wonder how the following code is evaluated: if ($data = somefunc())
Logic tells me that the most inner bracket command is carried out first,
then working outwards.
So the return value of somefunc() is assigned to $data even if this value is FALSE.
Then the if() condition is evaluated.
If PHP was strictly typed then $data could not be a boolean or say an array.
So does loosely typed allow potential bugs to creep in
You're correct, I think. The end value of the expression is used. You see, this is where the ambiguity comes in. If I write it as if (($data = somefunc())), then it makes perfect sense that the result from the innermost expression is used.
/doh.
@Dormilich
No. My understanding is that an assignment is an expression.
Edit: I guess not.
A note to Netbeans users, the warning can be disabled in
Tools>>Options then Hint tab. Select PHP for Language.
Uncheck the "Possible accidental assignment..." option under Stable.
This of course does not mean it is acceptable practice.
I checked the "Experimental" option under hints which revealed a lot more sins in my code.
Mainly unitialised variables, but I can live with those.
Here is a good reply that I got:
Assignment operations in PHP have the "side effect" of returning the
assignment. For example:
function return_false() {
return false;
}
var_dump(return_false()); //bool(false);
var_dump($a = return_false()); //bool(false);
var_dump($a = 1); // int(1)
var_dump($a = "hello world!"); //string...
So the same thing that allows you to do:
$a = $b = $c = $d = 154;
which works because "$d = 154" returns 154, which is assigned to $c,
which returns 154... is how assignment in conditionals or looping
works:
if($a = return_false()) { }
var_dump($a); //bool(false)
if($a = "hello") {}
var_dump($a); //string, "hello"
So what's really happening is the return value of the expression "$a =
____" is evaluated and that's used to determine the truth of the
conditionality. if($a = return_false()) is exactly the same thing as
if(return_false()) save for you "capture" the output of the function,
rather than just allow the conditional operator to see it. It's
functionally equivalent to $a = return_false(); if($a) {} but it's
important to understand that __assigning a variable to a value in PHP
is an expression with a return value___ and that return value is the
value that you assigned to the variable.
As I come from a C++ background,
in my own functions if I wish to return a value and/or an error code,
I prefer to pass a pointer to the function and return the error code - $data = array();
-
if(getData($data)){....}
-
-
function getData(&$retData){
-
$success = false;
-
...
-
//load data into $retData
-
//If successful then $success = true;
-
...
-
return $success;
-
}
But PHP inbuilt functions don't do it this way.
Very few even accept a pointer as a parameter.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jacek Generowicz |
last post by:
I am dealing with an application which reads in configurations by
calling (a wrapper around) execfile. Any configuration file may itself
execfile other configuration files in the same manner.
I...
|
by: Mark Twombley |
last post by:
Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error...
|
by: bearophileHUGS |
last post by:
The current version of ShedSkin (http://shedskin.sourceforge.net/
experimental Python to C++ compiler) is rather alpha, and the
development work is focused on debugging and implementing some more...
|
by: Paul Steckler |
last post by:
Here's some code that's giving me differing results, depending
on the compiler.
typedef foo {
int A,B;
} FOO;
int main() {
|
by: Uday Deo |
last post by:
Hi everyone,
I am looping through 4 nested loops and I would like to break in the inner
most loop on certain condition and get the control on the 2 nd loop instead
of 3rd loop.
Here is briefly...
|
by: Fredrik Tolf |
last post by:
Hi list!
I'm relatively new to Python, and one thing I can't seem to get over is
the lack of in-expression assignments, as present in many other
languages. I'd really like to know how Python...
|
by: Simon Burton |
last post by:
I've been doing a little c programming again (ouch!) and it's just hit
me
why python does not allow assignment inside expressions (as in c):
because it is absolutely essential that all assignments...
|
by: Brian Blais |
last post by:
Hello,
I have a couple of classes where I teach introductory programming using Python. What
I would love to have is for the students to go through a lot of very small programs,
to learn the...
|
by: MMcCarthy |
last post by:
Policies below superceded by FAQ Post Course Work Questions and Answers.
ADMIN
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |