Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A php if statement with one equal sign...? What does this mean?

Tags:

php

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!!!

like image 768
FrustratedPHPnovice Avatar asked Oct 05 '09 02:10

FrustratedPHPnovice


People also ask

What does the == sign do in an if statement?

When using ==, if the two items are the same, it will return True. Otherwise, it will return False.

What does a single equals symbol in JavaScript indicate?

Single Equal (=) in JavaScript We use Single Equal sign to assign value to the variable or to initialize an object. For example. var t = new Date(); //Variable assignment var amount = 100; //Object initialization var t = new Date();

What are PHP conditional statements?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

How do I check if two variables are equal in PHP?

The comparison operator called as the Identical operator is the triple equal sign “===”. This operator allows for a much stricter comparison between the given variables or values. This operator returns true if both variable contains same information and same data types otherwise return false.


2 Answers

It's a form of shorthand, which is exactly equivalent to this:

$confirmation = $payment_modules->confirmation();
if ($confirmation) {

}
like image 54
nickf Avatar answered Oct 21 '22 14:10

nickf


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
}
like image 40
Joel Avatar answered Oct 21 '22 13:10

Joel