What do the two equal signs mean when not being used to compare?
$saveOrder = $listOrder == 'a.ordering';
I've never seen anything like this in php.... I am looking at the weblinks Joomla 1.7 admin component.
Thanks
It is used for comparing. Except the result of the comparison is assigned to $saveOrder.
The following code:
<?php
list($listOrder1, $listOrder2) = array('a.ordering', 'a.something_else');
$saveOrder1 = $listOrder1 == 'a.ordering';
$saveOrder2 = $listOrder2 == 'a.ordering';
assigns true to the $saveOrder1 variable and false to the $saveOrder2 variable. If you do not believe, check for yourself here.
They are comparing. It's just not wrapped in parenthesis (like a comparison expression with if/while/etc).
$saveOrder will be assigned either true or false (the result of the condition).
I guess it is the same as $saveOrder = ($listOrder == 'a.ordering');
In your statement also the double equal sign(==) used for the comparison purpose only. Actually your statement contains both the 'assignment'(=) and 'comparison'(==) operators which leads to your confusion.
That is equivalent to $saveOrder = ($listOrder == 'a.ordering');, so first compares the $listOrder with 'a.ordering' and assign the result(true or false) to $saveOrder.
Hope this clear you confusion, if not let me know once.
$listOrder1='a.ordering';
$listOrder1='wrong'
$saveOrder1 = $listOrder1 == 'a.ordering';//1
$saveOrder2 = $listOrder2 == 'a.ordering';//
You can see the output when printing the first will be 1 whereas the second will return: (i.e. nothing)
Related
I have a stupid question about this condition.
Why when I put parenthesis, the result of the condition changes?
$std = new \stdClass();
$std->bool = false;
$resultCond1 = isset($std->bool) and $std->bool == true;
$resultCond2 = (isset($std->bool) and $std->bool == true);
var_dump($resultCond1); // True.
var_dump($resultCond2); // False.
I believe this is due to operator precedence.
Notice in that table that the assignment operators lie firmly between and and &&. Here's what I think is happening:
In the first example, isset is returning true and, prior to the and operation taking place the assignment is happening. After the assignment, the result of the assignment is and'ed and the result of that and operation is then summarily discarded.
In the second example the parentheses dictate that the assignment happens last and so you get the expected result.
You can see this more clearly if you remove the assignment operation altogether and just dump the result of the operations themselves:
var_dump(isset($std->bool) and $std->bool == true); // bool(false)
var_dump((isset($std->bool) and $std->bool == true)); // bool(false)
Both of these conditions are not outputting same result because of operator precedence.
1) For the first one - isset($std->bool) returns true, after that it will check and $std->bool, lastly it will compare that result with true
2) For the second one - it will check isset($std->bool) and $std->bool == true separately. Then compare both of these result.
Second one is more convenient & cleaner way to accomplish this type of work.
I have a line of code that scans a dataset:
dataset:
+117251093918
+1174418217128
0112347063455555
php line of code:
if (substr($row['someData'],0,3) == "011")
{
//do stuff
}
+117251093918
+1174418217128
0112347063455555
I would expect to find 1 result in this dataset, however all 3 results are found according to php. any thoughts as to why?
If you want to make sure you compare strings on both sides, use a triple =:
if (substr($row['someData'],0,3) === "011")
That way php will not convert any variable types to make the comparison work; both content and variable type now have to be equal.
In your case both sides are converted to integers, see the following example:
var_dump("+11" == "011");
returns true
and this example:
var_dump("+11" === "011");
returns false.
Check the manual for more details.
You should use ===. The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
In the example below, the first for loop shows why it returns good. The === exhibits typesafe comparison, it means unless the two values are of the same type and equal, it will return false. Whereas, == returns true regardless of the types, it just has to be equal.
<?php
// assume your row['somedata'] is this
$r = +11;
//returns good
if ((substr($r,0,3)) == "+11")
{
echo "Good";
}
//doesn't return any
if ((substr($r,0,3)) === "+11")
{
echo "Good One";
}
?>
I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.
print "*".$contactId."*<br/>";
if($contactId != '')
{
//queryContact($contactId);
print "Contact Present<br/>";
}
result returned to screen is:
**
Contact Present
If you want to see exactly what your string is, simply use var_dump(), like this, for instance:
var_dump($contactId)
instead of
print "*".$contactId."*<br/>";
Couple of things you can try:
if (!empty($contactId)) {
// I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
// I have a contact id
}
In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).
Do it with if (isset($contactId)) {}.
You likely want:
if (strlen($contactId))
You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php
and here: http://us3.php.net/manual/en/language.types.null.php
In future, use if(!empty($str)) { echo "string is not empty"}.
I'm attempting to troubleshoot a problem, and need to understand what this if statement is saying:
if ($confirmation = $payment_modules->confirmation()) {
All the resources I can find only show if statements with double equal signs, not single. Is this one of the shorthand forms of a php if? What is it doing?
(If it's actually wrong syntax, changing it to a double equal sign doesn't resolve the problem. As-is, in some scenarios it does return true. In the scenario I'm troubleshooting, it doesn't return true until after I refresh the browser.)
Any help is greatly appreciated!!!
It's a form of shorthand, which is exactly equivalent to this:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
}
This will first assign the value of $payment_modules->confirmation() to $confirmation. The = operator will evaluate to the new value of $confirmation.
This has the same effect as writing:
$confirmation = $payment_modules->confirmation();
if ($confirmation) {
// this will get executed if $confirmation is not false, null, or zero
}
The code works because an assignment returns the value assigned, so if $payment_modules->confirmation() is true, $confirmation will be set to true, and then the assignment will return true. Same thing for false.
That's why you can use a command to assign to many variables, as in a = b = 0. Assigns zero to b and returns that zero. Therefore, it becomes a = 0. And a receives zero and it will return that zero, which can or can not be used.
Sometimes people like to do an assignment and then check if the assignment went through okay. Pair this up with functions that return false (or equivalent) on failure, and you can do an assignment and a check at the same time.
In order to understand this, remember that assignments are a kind of expression, and so (like all expressions) have a return value. That return value is equal to whatever got put into the variable. That is why you can do something like
a = b = c = 0;
to assign all of those variables at the same time.
= means assignment ( $a = 1 ), == is for comparison ( true == false is false ). I think in your example it should use = because it assigns it to the return value of confirmation, which should be something that evaluates to true.
Try doing a var_dump:
var_dump( $payment_modules->confirmation() );
See what boolean it evaluates to, and from there you can troubleshoot. Post more code if you want more help.
class test() {
public function confirmation() { return true; }
}
$boolean = test::confirmation();
var_dump( $boolean );
Will equate to true
Im using an if statement to determine what to return in a function, but it seems to be not working the way i want it to.
function DoThis($dogs, $cats){
// do something with dogs, pet them perhaps.
$reg = $dogs[0];
$nate = $dogs[1];
if($cats = "dave"){return $reg;}
if($cats = "tom"){return $nate;}
}
$cats is a string (if that helps), and when entered it doesn't yield any return.
If i manually set a return, that works, but the above doesnt for some reason.
To test for equality, use the == (double equals) operator instead of the = (single equals) operator.
For example:
if("dave" == $cats){return $reg;}
if("tom" == $cats){return $nate;}
You're using the assignment operator instead of the comparison operator. Try the following instead.
$cats == "dave"
$cats == "tom"
When you say
if($cats = "dave") { ... }
you're really saying
Assign the value "dave" to the variable $cats
If the variable $cats is true after assignment, return true. Otherwise, return false
It's a common mistake, and something tha plagues old hands and new hands alike.
You need to use == to compare.
= is an assignment, so it has the effect of setting $cats to "dave" and then (because the expression evaluates to "dave", which is non-empty) it treats the if statement as being "if (true) ..." and executes the contained code.