简体   繁体   中英

two equal signs in php when NOT comparing

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).

我想这与$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: (ie nothing)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM