/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP version of "undefined || literal"

In JavaScript, when I’m not sure if a variable is defined, I can say

[CODE]alert(myVar || “Something else”)[/CODE]

If the variable is undefined, it will obviously return “Something else”.

In PHP, I have been using isset() and the ternary operator (??

[code=php]echo isset($myVar) ? myVar : ‘Something else’;[/code]

This seems kind of redundant, and I was hoping there was something similar in PHP.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@jamesbcox1980authorDec 22.2009 — I guess maybe what I should be looking at is how to declare undefined in PHP. I forgot that I usually insert "var undefined;" in my JS template to make this work. I guess I need to do the same in PHP... but declaring variables is a little different.
Copy linkTweet thisAlerts:
@NogDogDec 22.2009 — Offhand I don't know of a terser way than the tenary operator for this sort of thing.

As far as "defining" a variable as undefined, you can set it to null, which isset() will treat as not set.
[code=php]
var_dump(isset($var)); // false
$var = null;
var_dump(isset($var)); // false
$var = false;
var_dump(isset($var)); // true
[/code]
Copy linkTweet thisAlerts:
@jamesbcox1980authorDec 22.2009 — Well, I'm not looking to define a variable as undefined, I'm trying to set undefined to null by declaring it.

In JavaScript,[CODE]var undefined;[/CODE] takes a reserved word, undefined, and defines it, so that whenever a variable is undefined and gets called, it will return "undefined" instead of throwing an undefined variable error. This way, you can use the "||" operator instead of using the whole typeof() thing. It's not a strict standards compliant method, but it works and makes for easier error handling. Even jQuery uses it.
Copy linkTweet thisAlerts:
@NogDogDec 22.2009 — Nope, nothing like that in PHP that I've ever seen.
Copy linkTweet thisAlerts:
@MindzaiDec 22.2009 — what happens when myVar is false? Or some other correct value which evaluates false?
Copy linkTweet thisAlerts:
@KorDec 22.2009 — The PHP's Boolean operators [I]always[/I] return a Boolean value... as opposed to other languages that return the value of the last evaluated expression.

But you may do something tricky like:

[code=php]
$echo=$myVar or $echo="something else";
echo "$echo";
[/code]

It will work with a notable exception: when $myVar=0. In this case the assignment $echo=$myVar is evaluated as Boolean [B]false[/B].

Or you may simplify the ternary operator like this:
[code=php]
echo ($myVar?$myVar:"something else");
[/code]

But, again, $myVar can have any value except 0, because 0 is evaluated as Boolean false.
Copy linkTweet thisAlerts:
@MindzaiDec 22.2009 — 
But, again, $myVar can have any value except 0...[/QUOTE]


or "", or "0", or an empty array, or null etc. This technique seems dodgy enough in javascript! Just use isset() to see if a variable has been defined.
Copy linkTweet thisAlerts:
@KorDec 22.2009 — or "", or "0", or an empty array, or null etc. This technique seems dodgy enough in javascript! Just use isset() to see if a variable has been defined.[/QUOTE]
I agree. So, [B]jamesbcox1980[/B], the answer is no. As I said, php always return a Boolean value on Boolean operators.
Copy linkTweet thisAlerts:
@jamesbcox1980authorDec 22.2009 — Ok, thanks guys
×

Success!

Help @jamesbcox1980 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...