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.
Related
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }
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
}
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??
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 6 years ago.
(PHP)
for example, i have functions,which execute heavy queries and return TRUE or FALSE.
is there any performance difference:
if ( cond1() && cond2() && cond3() && cond4())
or
if (cond1()){
if (cond2()){
}
}
because, in the last case, if cond1() is false, then it doesnt execute the other conditions.. is same for the first example?
No this has no performance differences.
In this example when condition 1 fails, it doesn't execute the second condition
if ( cond1() && cond2() && cond3() && cond4())
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP startsWith() and endsWith() functions
Check if variable starts with ‘http’
How make an if-statement that if $mystring has the prefix "http://" then {do something}.
I've done this in objective-c like this:
if([mynsstring hasPrefix:#"http://"])
{
//Do something...
}
I don't know how to do this in PHP.
Thanks for your help in advance.
Simplest would be using substring to compare
if (substr($mystring, 0, 7) === 'http://') {
// do something
}
Remember of course to take the exact number of characters.