Asked  7 Months ago    Answers:  5   Viewed   31 times

In php, ($myvariable==0) When $myvariable is zero, the value of the expression is true; when $myvariable is null, the value of this expression is also true. How can I exclude the second case? I mean I want the expression to be true only when $myvariable is zero. Of course I can write

($myvariable!=null && $myvariable==0 )

but is there other elegant way to do this?


$myvariable === 0

read more about comparison operators.

Wednesday, March 31, 2021
answered 7 Months ago

PHP 7.1 Now supports nullable return types. The first RFC I linked to is the one they went for:

function nullOrString(int $foo) : ?string
    return $foo%2 ? "odd" : null;

old answer:

Since my comment was actually an answer to the question:

PHP 7 won't support nullable return-types just yet, but there's an RFC out to address just that, it aims to land in PHP 7.1. If it passes, the syntax would then affect all type-hints (both return types and type-hints):

public function returnStringOrNull(?array $optionalArray) : ?string
    if ($optionalArray) {
        return implode(', ', $optionalArray);//string returned here
    return null;

There's also a competing RFC to add union types, which would be able to do the same thing, but would look different:

public function returnStringOrNull(array|null $optionalArray) : string|null
    if ($optionalArray) {
        return implode(', ', $optionalArray);//string returned here
    return null;

For now, though, you'll have to write:

public function returnStringOrNull( array $optionalArray = null)
    if ($optionalArray) {
        return implode(', ', $optionalArray);

Or just return an empty string to be consistent with the return type, and check falsy value:

public function returnStringOrNull( array $optionalArray = null) : string
    if ($optionalArray) {
        return implode(', ', $optionalArray);
    return '';
$string = $x->returnStringOrNull();
if (!$string) {
    $string = $x->returnStringOrNull(range(1, 10));
Wednesday, March 31, 2021
answered 7 Months ago

Your problem is probably in using || instead of &&.

You want it to echo if you are not on page 92 AND you are not in a subpage of page 92.

Let's say you're on page 92, then your current code does this:

if (false || true)

because 92 is not a parent of page 92. Thus, since one condition is true, it triggers.

If you're on a subpage of 92, then it's the opposite:

if (true || false)

If you're on a page that isn't 92 or a subpage of 92, then you get:

if (true || true)

So, it will always trigger, regardless of what page your on, because || requires only a single true statement for the entire condition to be true.

Hence, change your code to

<?php if (!is_page(92) && $post->post_parent !== 92) { echo $foo; } ?>

Which gives a logical run down like:

Page 92:

if(false && true) //false, won't trigger

Subpage of 92:

if(true && false) //false, won't trigger

Some unrelated page:

if(true && true) //true, will trigger

Saturday, May 29, 2021
answered 5 Months ago

It's language specific, but in PHP :

Null means "nothing". The var has not been initialized.

False means "not true in a boolean context". Used to explicitly show you are dealing with logical issues.

0 is an int. Nothing to do with the rest above, used for mathematics.

Now, what is tricky, it's that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is False.

If you test it with ==, it's testing the boolean value, so you will get equality. If you test it with ===, it will test the type, and you will get inequality.

So why are they useful ?

Well, look at the strrpos() function. It returns False if it did not found anything, but 0 if it has found something at the beginning of the string !

// pitfall :
if (strrpos("Hello World", "Hello")) { 
    // never exectuted

// smart move :
if (strrpos("Hello World", "Hello") !== False) {
    // that works !

And of course, if you deal with states:

You want to make a difference between DebugMode = False (set to off), DebugMode = True (set to on) and DebugMode = Null (not set at all, will lead to hard debugging ;-)).

Saturday, June 5, 2021
answered 5 Months ago

That is because PHP null is converted into the empty string "" when you create the query string.

$variable = null;
$insert = "insert into mytable set mycolumn = $variable" ;
echo $insert;

Will produce:

insert into mytable set mycolumn = 

To fix your query you would need to check if the PHP variable is null and change it to string NULL. (Also now mentioned in the comment of @MarkB.)

if ($variable == null){
    $variable = "NULL";

This will produce:

"insert into mytable set mycolumn = NULL"

Note that NULL has no " around it because it is now concatenated to the other string.

*(note: insert into tablename set .. is not correct, you either insert data or you update tablename set data.)

Saturday, August 28, 2021
Igor Tupitsyn
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :