سؤال

function test(){
$embedmode = 'normal';
if ( ( $embedmode != '' ) && ( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' ) )
    return "<p>ARVE Error: mode is not set to 'normal', 'popup' or 'special' maybe typo</p>";
elseif ( $embedmode == '')
    $mode = 'default';
else
    $mode = $embedmode;

echo '<pre>';
var_dump($mode);
echo "</pre>";
}
echo test();

this is my attempt and I am getting a headache now it puts out the return message and I don't know why

هل كانت مفيدة؟

المحلول

Your bad logic is here:

( $embedmode != 'normal' || $embedmode != 'popup' || $embedmode != 'popup' )

if $embedmode equals 'normal', then $embedmode != 'popup', so this whole bit is TRUE. I believe you want to replace || with &&.

For code that's easier to reason about, I'd probably use in_array or switch, like this:

switch ($embedmode) {
  case 'normal':
  case 'popup':
  case 'special':
    // valid, proceed
    break;
  case '':
    $embedmode = 'default';
    break;
  default:
    trigger_error ("Embed mode '$embedmode' not valid.");
    break;
}

نصائح أخرى

!($var == 'something') would be the same as ($var != 'something').

Doing (!$var == 'something') would perform the boolean operation on $var before doing the comparison. !$var would return false unless $var is empty, so it would essentially be saying (false == 'something'), which would be false.

1) $var != 'something' says "$var IS NOT 'something'".

2) !$var == 'something' says "THE NEGATION OF $var IS 'something'".

The expressions mean different things. Test it with saying $var is 'foobar', your two sentences are:

1) 'foobar' IS NOT 'something', and

2) THE NEGATION OF 'foobar' is 'something'

As you can see, 1) will return true, yet 2) will return false, because the negation of whatever 'foobar' is, it is still not equal to 'something'.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top