문제

Is there a shorthand version for, "if this variable isn't set yet, then set it"?

Example:

switch ($Frequency) {
    case 'Once':
        doSomethingSpecific();
        break;
    case 'Daily':
        $Message = 'Event will occur every day at the same time.';
    case 'Weekly':
        if (!isset($Message)) $Message = 'Event will occur every week on the same day of the week, at the same time.';
    case 'Monthly':
        if (!isset($Message)) $Message = 'Event will occur every month on the same day of the week.';
        doSomething($Message);
        break;
}
도움이 되었습니까?

해결책

Yes it's called the ternary operator:

$var = isset($var) ? $var : "Some default";

Since PHP 5.3 there's a shorthand version for this as well:

$var = isset($var) ?: "Some default";

And an alternate version for PHP 5.3+:

isset($var) ?: $var = "Some default";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top