Jump to content

Question about || in PHP


The Bat

Recommended Posts

Hello,

 

I find myself using quite a bit of (example):

 

function say_greeting($greeting = null) {
   $greeting = ($greeting) ? $greeting : 'Hello!'
   echo $greeting;
}

 

in my code (return $greeting if set, otherwise return 'Hello!'), and it's certainly not very DRY. In Ruby, a simple:

 

def say_greeting(greeting = nil)
   print greeting || 'Hello!'
end

 

does the trick. I'm wondering if I can do something similar in PHP instead of what I am doing? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/107916-question-about-in-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.