Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP or operator similar to javascript? Is there a simple way?

Tags:

php

In javascript I can pass an object literal to an object as a parameter and if a value does not exist I can refer to a default value by coding the following;

this.title = params.title || false;

Is there a similar way to do this with PHP?

I am new to PHP and I can't seem to find an answer and if there is not an easy solution like javascript has, it seems pure crazy to me!!

Is the best way in PHP to use a ternary operator with a function call?

isset($params['title']) ? $params['title'] : false;

Thanks

like image 651
GriffLab Avatar asked Nov 19 '25 13:11

GriffLab


1 Answers

Don't look for an exact equivalent, because PHP's boolean operators and array access mechanism are just too different to provide that. What you want is to provide default values for an argument:

function foo(array $params) {
    $params += array('title' => false, ...);

    echo $params['title'];
}
like image 61
deceze Avatar answered Nov 21 '25 04:11

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!