I was doing some self-learning about cakephp (version 1.26).
I got a simple HTML input text field like this:
<input type="text" name="data[testing][name]" id="data[testing][name]">
The value from the Input text box field was checked against the database.
If the value matches the data stored in the database, it will return true.
Here is the code:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result){ //doing something;}
}
I came across a question when I altered the code above with a little change,
but then it failed to work then:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result===true){ //doing something;}
}
Could anyone help please?
You are using strict type comparison with === rather than ==, this implies that $result is actually not equal to true there by making the condition fail. Try to see what does come though in the $result variable:
var_dump($result);
Or try this condition with (==):
if($result == true){ //doing something;}
Or simply:
if ($this->User->findByname($t['testing']['name'])){ //doing something;}
Assuming here that findByName returns some kind of object or array. if you use if ($result) this object/array will be cast to a boolean.
If however you use if ($result === true) you're strictly comparing an object/array to the boolean true, this comparison will always evaluate to false.
The PHP reference has a very nice explanation of how type comparison is done. The quick answer is that you are now doing a much stricter comparison, and some edge cases are falling through the cracks. You will probably be fine with $result == true
http://php.net/manual/en/language.operators.comparison.php
That's because $result === true checks, if $result value is true.
But your $result variable contains results from the database.
Coarsely, the if operator casts your argument into a boolean and evaluates it. So if($result) converts $result into true or false. On the other hand, === actually checks for both type and "value" equality, so true === $val will only return true if $val is the boolean true. === obviously returns a boolean, so no casting is necessary for the subsequent evaluation within if. What this means for you is that if($result) processes the block if $result casts into true. Examples of things that become true are 1, '1', and new Object(). Conversely, if($result===true) doesn't immediately cast $result. It checks it in type and "value" against boolean true.
If $result is, say 1, the former control structure will process the block, but the latter won't.
=== means equal AND the same type of what you equaling to...
but $result contains data from db..so it's not Boolean...
use == instead:
if($result==true)
In your code, when there is a result, the return is non-zero, therefore it will evaluate to true.
=== is the identity operator and will return true when the two objects are identical
e.g. 1===1 (true) true===true (true) true===1 (false)
== is the equality operator and will return true when the two objects are equal or equivalent.
e.g. 1==1 (true) true==true (true) true==1 (true)
findByName will return an array or unset. unset will equate to false and an array will equate to true.
the value true itself is never returned in your code, so === will never be true.
Related
I have a simple requirement , In logic app trigger I am getting a variable called suspendschedule and I want to make
if(suspendschedule == true then value should be Yes
if suspendschedule == false then the value should be No
and this is what I am trying
#if(equals(triggerBody()?['_suspendschedule],"false"),"No","Yes")
if your suspendschedule is a type of bool then Try these
first, check for the true condition because if the field is null or missing in triggerBody it will go in else
#if(equals(triggerBody()?['suspendschedule'],bool(1)),'Yes','No')
if it is in string format or you are not sure try this it will check both bool and string
first, check for the true condition because if the field is null or missing in triggerBody it will go in else
#if(equals(triggerBody()?['suspendschedule'],'true'),'Yes','No')
Can someone expalain this code to me? I am getting a (false && false && why are you comparing a hyperlink markup?
{id === constants.ACCOUNT_NO && sessionState['backBtnAddressDetails'] &&
Confirm details }
The logic that the chained &&s implement is: the final expression in the chain, that is, the
Confirm details
gets rendered only if the prior two expressions are truthy.
Another way of doing the same thing would be:
{showConfirmLink()}
const showConfirmLink = () => {
if (id !== constants.ACCOUNT_NO || !sessionState['backBtnAddressDetails']) {
return null; // don't render anything
}
return Confirm details;
};
Another example of this, outside of React:
const theValue = 'abc' && true && 'theValue';
console.log(theValue);
If any of the expressions in an && chain are falsey, the chain stops evaluating there, and the whole thing resolves to that falsey expression. Otherwise, it evaluates to the final expression in the chain.
This type of expression is referred to as conditional rendering in React ,
basically if the condition is true, the element right (the Confirm details element in your case ) after && will appear in the output. If it is false, React will ignore and skip it.
it could mean the id and constants.ACCOUNT_NO aren't of the same type (when you use the === operator it strictly compares the types too )
another possibility is sessionState['backBtnAddressDetails'] could be null
i want to check if the url contains ?query_param if so then get its value and compare that value to an id.
consider the url /path/20?query_parm=2234
and i have to get the param_id and compare it with the item id.
so i do something like below,
handle_location = (path) => {
let opened_item, param_id;
param_id = new
URLSearchParams(this.props.location.search).get('query_param');
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id);
}
};
the data structure for items is below,
items = [{
id: 2244;
attributes: something;
}
{
id: 33;
attributes: nothing;
}]
But this gives the opened_item value undefined since item.id is never equal to param_id... because of type being different.
How can i fix this...or is there a better way to find the query_param from url and get its value and use it accordingly to find the item that matches with the query_param value.
Given you understand that both data types are different, you could use avoid using strict equality and leverage type coercion which would work
item.id == param_id
The most efficient way though would be to convert param_id to the appropriate type before comparing e.g.
param_id = parseInt(param_id, 10);
It means one conversion and you can keep the strict equality
You will need to either cast both of the values to the same type(either Number or String) and then perform the comparison or you could use == operator which will try to coerce the types automatically(not recommended). You can also always fall back to some default value if none of the items matched the id.
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id) || 'some default value'
}
try this:
const param_id = this.props.match.params.id
I'm using angularjs and trying to set up a flag on the front end for my ng-if. I wanted to consolidate the logic in the controller but the the ng-if is only returning true and never false.
I need some efficient way for my code to return false if conditions are not met instead of returning undefined.
vm.showLocButton = !vm.isSupervisorReviewApp && vm.application.benefitPeriod.program.programType.id === vm.constants.programTypeId.directBill && vm.application.applicationStatus.code === vm.constants.applicationStatus.locIssued;
Dont know exactly but I see '=' instead of '==' are you assigninv the value or comparing if comparing than try using '=='
vm.showLocButton == !vm.isSupervisorReviewApp && vm.application.benefitPeriod.program.programType.id === vm.constants.programTypeId.directBill && vm.application.applicationStatus.code === vm.constants.applicationStatus.locIssued;
It will really helpful if you can post the entire code.
I have following code:
ignored := [ "Rainmeter.exe", "Nimi Places.exe", "mumble.exe" ]
a := ignored.HasKey("mumble.exe")
MsgBox,,, %a%
It returns 0 even though the string is clearly present in the array.
How do I test if a string value is present in an array?
PS: I also tried if var in which gives same results.
You can't, using just one command. Such functionality is not implemented in AHK_L as of 1.1.22.3.
You'll have to either define your own function
hasValue(haystack, needle) {
if(!isObject(haystack))
return false
if(haystack.Length()==0)
return false
for k,v in haystack
if(v==needle)
return true
return false
}
or use some fancy workaround:
ignored := { "Rainmeter.exe":0, "Nimi Places.exe":0, "mumble.exe":0 }
msgbox, % ignored.HasKey("mumble.exe")
This would create an associative array and put your values as keys (the values are set to 0 here), so the .HasKey() makes sense to use.