Set variable in if statement expression

后端 未结 2 1048
天命终不由人
天命终不由人 2021-02-12 11:53

I ran across some interesting code today. I tried to find out if this is a feature of PHP or if I am missing something, but was unable to find anything on Google. Probably becau

2条回答
  •  青春惊慌失措
    2021-02-12 12:19

    You can make an assignment like that in a conditional. What happened logically is that the value is assigned to $logo and then $logo is evaluated as to whether it is truthy. If it is truthy, the code in the conditional executes.

    You will oftentimes see this sort of assignment/evaluation in the case of looping through database result sets, but generally I would suggest that it should be avoided outside such a common use case for sake of clarity in reading g code.

提交回复
热议问题