PHP checking a variable with empty() or truthy [duplicate] - php

This question already has answers here:
PHP: using empty() for empty string?
(4 answers)
should i use empty() php function
(2 answers)
Closed 4 years ago.
Can anyone tell me the different between checking if a variable has value using the empty() function or "truthy" in an if statement? As far as I can tell they achieve the same thing?.. I am writing a function for Wordress that loops through the taxonomies and both work but I am not sure which is best.
$taxonomies = get_taxonomies($args, $output, $operator);
if (!empty($taxonomies)) {
...code here
}
or
$taxonomies = get_taxonomies($args, $output, $operator);
if ($taxonomies) {
...code here
}

Related

Why something like this is invalid in php? [duplicate]

This question already has answers here:
Using return in ternary operator
(7 answers)
Closed 5 years ago.
I was recently refactoring. And thought this would awesome. But is not allowed.
<?php
function(){
doSomething() && return true;
}
Because return is a statement and cannot be used as part of an expression. An expression is something that returns/results in a value. return doesn't result in any value, but operands to && must result in a value for the && expression to be evaluable.

Checking in IF statement [duplicate]

This question already has answers here:
Order in conditional statements [duplicate]
(2 answers)
Closed 5 years ago.
What is the difference between the following two statements?
Suppose we have the following variable.
$someVariable = false;
Statements;
if($someVariable === false)
and
if(false === $someVariable)
There is no difference you can use it any way you want.You are just checking/comparing 2 values whether they are equal or not.
In simple words it's just like Is 1+2=3 or 2+1=3??

PHP identical arrays return false on check [duplicate]

This question already has answers here:
recursive array_diff()?
(5 answers)
Closed 6 years ago.
I am checking whether two arrays are identical and even though I know they are, my conditional returns false.
Here are the two arrays: http://pastebin.com/knekiW67
Here is the code:
$stored_items = (Array1 in Pastebin link)
$new_items = (Array2 in Pastebin link)
if($stored_items === $new_items) {
return true;
} else {
return false;
}
I've even checked the two arrays using https://www.diffchecker.com/ and it responds that they are identical.
Does anyone know why it's returning false?
=== if not working for array(only string and int)
for checking array need use http://php.net/manual/ru/function.array-diff-ukey.php with special callback

Using current variable function inside the function [duplicate]

This question already has answers here:
Anonymous recursive PHP functions
(6 answers)
Closed 9 years ago.
i need to recursively call a variable function, how can i do this?
$rec = function($li) use($html,$rec) { // error, rec was not defined yet
if( ... ) $rec( ... );
}
how can i do this?
Use the function variable $rec by reference (&$rec) so you can set it to the function then. This will also define it.
use($html, &$rec)
^
You find this principle outlined in the question Anonymous recursive PHP functions.

An Ampersand (&) before a function means? [duplicate]

This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I know & is used to create references.
But I wonder what having a & before a function name means:
function &DB($params = '', $active_record_override = FALSE) { // code }
It returns the result by reference. See the manual entry for it here.

Categories