40

For example, can I do:

if ($my_array = wp_get_category($id)) {
    echo "asdf";
} else {
    echo "1234";
}

If nothing is returned by the function, I want to go into the else statement.

Casey
  • 2,611
  • 6
  • 34
  • 60

6 Answers6

63

Yes, that will work, and the pattern is used quite often.

If $my_array is assigned a truthy value, then the condition will be met.

CodePad.

<?php

function wp_get_category($id) {
   return 'I am truthy!';
}

if ($my_array = wp_get_category($id)) {
    echo $my_array;
} else {
    echo "1234";
}

The inverse is also true...

If nothing is returned by the function, I want to go into the else statement.

A function that doesn't return anything will return NULL, which is falsey.

CodePad.

<?php

function wp_get_category($id) {
}

if ($my_array = wp_get_category($id)) {
    echo $my_array;
} else {
    echo "1234";
}
alex
  • 479,566
  • 201
  • 878
  • 984
  • 7
    As important the variable is available outside of the scope of the if... http://codepad.org/Bog9U9kL – Toaster Mar 15 '13 at 16:08
5

This is in fact a common pattern and will work. However, you may want to think twice about using it for more complex cases, or at all. Imagine if someone maintaining your code comes along and sees

if ($x = one() || $y = two() && $z = three() or four()) {

}

It might be better to declare the variables before using them in the conditional.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
4

I found this wondering about the rules of declaring a variable then using it immediately in subsequent conditions in the same statement.

Thanks to previous answer for the codepad link, I made my own to test the theory. Spoiler alert: It works.

http://codepad.org/xTwzTwGR

Teeks
  • 94
  • 3
  • Interesting that it doesn't work without enclosing the declaration. – Rudiger Dec 19 '16 at 05:25
  • The parentheses are necessary to keep everything to the right of the "=" from grouping together. http://php.net/manual/en/language.operators.assignment.php – Isaac Lubow Feb 11 '19 at 18:12
4

you might want something like this:

if (!is_null($my_array = wp_get_category($id)) {
    echo "asdf";
else
    echo "1234";

Assuming the function returns null upon failure. You may have to adjust it a bit.

LainIwakura
  • 2,871
  • 2
  • 19
  • 22
2

Following is one more alternative to define any variable (with safety):

$my_array = ($my_array = $wp_get_category($id)) ?: /* else statement here */;
OO7
  • 660
  • 4
  • 10
0

I always found this principle confusing as it never seemed to work for me! Take the following code:

if ($pid = $arr['Key']){

This may throw an error Undefined index: Key. Equally I get the same result with this:

if (!empty($pid = $arr['Key']))

The solution now with PHP7+ is as follows:

if ($pid = $arr['Key'] ?? false)

Which will allow for an array with an empty value setting $pid = false; and not triggering the IF statement.

I hope that helps someone as arrays threw me but the double coalesc is super helpful and can be used in the if concept.

Antony
  • 3,875
  • 30
  • 32