Domanda

I have this code:

function basket_admin_tabs( $current = 'edit' ) {
    if ($current==null)
        $current='edit';

Is there a way for php to recognize null as no argument? Telling function the same information two times seem weird.

@edit

I don't know if I expressed myself correctly. The function is taking the argument to know wich tab to use. If argument isn't set or is null I want it to be 'edit'.

È stato utile?

Soluzione

No, null is just a valid value as a parameter.

You could set the default value if falsy like below:

function basket_admin_tabs( $current = 'edit' ) {
  $current = $current ? $current : 'edit';
}

// after php 5.3
function basket_admin_tabs( $current = 'edit' ) {
  $current = $current ?: 'edit';
}

Altri suggerimenti

This would suffice.

/**
 * defaults to 'edit'
 * 
 * @param null $current
 */
function basket_admin_tasks($current = null)
{
    $current = ($current == null ? 'edit' : $current);
}

Or this:

/**
 * @var string
 */
private $current = 'edit';

/**
 * @param $current
 */
public function basket_admin_tasks($current)
{
    $this->current = $current;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top