Request for PHP syntax best practice for ensuring function returns defined value

Project is cleaning up a WordPress theme to fix all debug log spew, quieting all syntax errors + warnings. I primarily work with PERL, so looking for best PHP best practices for code clean up.

First question…

When a function requires returning a defined value, PERL provides quick/clean way of ensuring variables are set, for example, if $count is unset, in PERL two quick options…

  1. $count ||= 0; # sets $count to 0 if $count is undefined

  2. return ( $count || 0 ); # returns 0 if $count is undefined

In PHP, this seems to work + while clear, seems verbose.

if (! isset($count)) { $count = 0; }

And someone let me know if there is a cleaner way to do this, preferably something like…

return ( $count || 0 );


Hi dfavor, welcome to the forum

I’m not sure if this will be of any help (cleaning up WordPress code sounds like a nightmare task) but default values can be assigned to function parameters. eg.

function return_count_value($some_param, $another_param, $count = 0) {
  return $count;


This works well for designing from scratch.

Unfortunately this code appears… well… organically designed over time…

Some functions have 100s to 1000s of lines of code, so clearest handling of undefined return values is at end of functions.

Luckily these functions, so far, have a single exit point.

It has been a while since I did any deep digging into WordPress code so things may have improved some.
At that time much code was a mess, and I’m talking Core code, not plugins or templates.

From what I see there are several reasons

  • WordPress wants to have backwards compatibility for users that have antiquated environments
  • WordPress wants to be all things to all users
  • WordPress seems to be more focused on adding new features at the expense of cleaning up existing code

If a function wasn’t written so as to ensure all variables are defined eg.

function some_function() { 
  $define_me = 0;

but instead like

function some_function() { 
  if ($some_condition_that_may_not_be_true) {
    $define_me = 0; 

poring through the literally thousands of functions to find such problems is not realistic.

PHP version 7 has some useful new features such as those for data types, but it is unlikely that the Core code will be taking advantage of those anytime soon.

If WordPress had unit tests it would be a big help, but again, not likely anytime soon.

I think the best way to approach the problem would be to set-up custom error and exception handlers and deal with them as you become aware of them.

Unfortunately this mishandles the case when a variable like $define_me is by the aggregate return values of many other functions… in the case where all return values aggregated turn out to be undefined from all other functions.

So fix up of this situation is best done just prior to returning the value upstream, to caller.

Where “best done” means most clear to people reading code in future.

So far, sounds like best approach is the snippet I posted above…

if (! isset($count)) { $count = 0; }

Yes, I think so, there is also ternary

$count = ( isset($count)  ) ? $count : 0;

And PHP 7 has null coalescing

$count = $count ?? 0;

Ah… so…

PHP-5.x - return isset($count) ? $count : 0;

PHP-7x - return $count ?? 0;

Got it. Thanks all.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.