PHP switch, why doesn't this work?

I have a strange problem that I cannot solve. I have quite complex code, but I simplified it and the problem still exists.

See the following:

<?php
$meta = array('meta_title' => 'correct');

switch (true) {
    case empty($meta['meta_description']):
        $meta['meta_description'] = 'incorrect';
    case empty($meta['meta_keywords']):
        $meta['meta_keywords'] = 'incorrect';
    case empty($meta['meta_title']):
        $meta['meta_title'] = 'incorrect';
}

print_r($meta);

      

Now for some reason it is returned by meta_title as an invalid event even though it is explicitly set in the array. It's almost as if it ignores the case and just falls down.

I created an example: http://codepad.org/mQH9Kf1L

Thanks in advance!

UPDATE

It might seem like I can use this. See the following: http://codepad.org/WnxBp8Nt (line 43 onwards)

Just out of interest, I changed, I added a quick micro-timer and tested this version and the version written with separate ifs. The if version came out a little slower.

+3


source to share


6 answers


The reason it doesn't do what you want is because if case 1 is true, cases 2 and 3 are triggered automatically (and if case 2 is true, case 3 always fires). It doesn't exist for that switch

. You just need 3 separate sentences if

:



<?php
$meta = array('meta_title' => 'correct');

if (empty($meta['meta_description']))
        $meta['meta_description'] = 'incorrect';
if (empty($meta['meta_keywords']))
        $meta['meta_keywords'] = 'incorrect';
if (empty($meta['meta_title']))
        $meta['meta_title'] = 'incorrect';

print_r($meta);

      

+3


source


Quoting from PHP Documentation for Switch :

It is important to understand how the switch statement is executed in order to avoid errors. The switch statement is executed line by line (in fact, a statement by statement). At the beginning of the code there is no executed. Only when a case statement is found with a value that matches the value of the switch expression that PHP starts executing the statements. PHP continues to execute statements until the end of the switch block or the first time it sees a break statement. If you do not write a break statement at the end of a case case list, PHP will continue executing the next case statements.



What you are actually trying to do is:

<?php
$meta = array('meta_title' => 'correct');

switch (true) {
    case empty($meta['meta_description']):
        $meta['meta_description'] = 'incorrect';
}
switch (true) {
    case empty($meta['meta_keywords']):
        $meta['meta_keywords'] = 'incorrect';
}
switch (true) {
    case empty($meta['meta_title']):
        $meta['meta_title'] = 'incorrect';
}

print_r($meta);

      

+2


source


Remember break

:

$meta = array('meta_title' => 'correct');

switch (true) {
    case empty($meta['meta_description']):
        $meta['meta_description'] = 'incorrect';
        break;
    case empty($meta['meta_keywords']):
        $meta['meta_keywords'] = 'incorrect';
        break;
    case empty($meta['meta_title']):
        $meta['meta_title'] = 'incorrect';
        break;
}

print_r($meta);

      

Also the above value doesn't make sense .

You shouldn't use a switch statement for the above.

Try using if...elseif...

:

if(empty($meta['meta_description']))
    $meta['meta_description'] = 'incorrect';
elseif(empty($meta['meta_keywords']))
    $meta['meta_keywords'] = 'incorrect';
elseif(empty($meta['meta_title']))
    $meta['meta_title'] = 'incorrect';

      

+1


source


Breaking up won't solve the problem. If / else won't work as many needs. Also your code doesn't make sense. Have you ever heard of foreach?

<?php
$MetaDefault = array('meta_description', 'meta_title', 'meta_keywords');

$Meta = array('meta_title' => 'correct');

foreach($MetaDefault as $Row){
    if(!isset($Meta[$Row])){
        $Meta[$Row] = 'incorrect';
    }
}

print_r($Meta);
?>

      

If you break it will shut down. Your other variables will not be checked. If / else will do the same.

+1


source


Once one case

is true, all of the following code in the switch block is executed, excluding other statements case

. See phpswitch

documentation for operator :

The switch statement executes line by line (in fact, a statement-statement). At the beginning, the code is not executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP start executing the statements. PHP continues to execute instructions until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of the list of case statements, PHP will continue to execute the next case.

So what happens here is the following:

switch (true) {
    case empty($meta['meta_description']): // MATCH
        $meta['meta_description'] = 'incorrect';  // EXECUTE
    case empty($meta['meta_keywords']): // SKIP
        $meta['meta_keywords'] = 'incorrect'; // EXECUTE
    case empty($meta['meta_title']): // SKIP
        $meta['meta_title'] = 'incorrect'; // EXECUTE
}

      

Note that the following statement bodies are case

not executed at all:

switch(true) {
    case false:
        echo "Not Executed\n";
    case true:
        echo "Executed\n";
    case print("Condition Not Executed\n"):
        echo "Also Executed\n";
}

      

This will print:

Executed
Also Executed

      

+1


source


You don't have operators break

. Add one to each occasion and it will work.

What happens when the first case ( empty($meta['meta_description'])

) is evaluated as true

and without any operators break

, the code for the rest of the cases is also executed, so it is meta_keywords

set incorrectly and therefore meta_title

.

Given that this is the way operators work switch

, you probably don't want to use an operator switch

. You are probably looking for something like this:

$meta = array('meta_title' => 'correct');

if (empty($meta['meta_description']))
    $meta['meta_description'] = 'incorrect';
if (empty($meta['meta_keywords']))
    $meta['meta_keywords'] = 'incorrect';
if (empty($meta['meta_title']))
    $meta['meta_title'] = 'incorrect';

print_r($meta);

      

Or you can use a loop:

$meta = array('meta_title' => 'correct');

$properties = array('meta_description', 'meta_keywords', 'meta_title');
foreach ($properties as $property)
{
    if (empty($meta[$property]))
        $meta[$property] = 'incorrect';
}

print_r($meta);

      

Both of them should give the same result, but the loop will be easier and shorter if you have more properties.

-1


source







All Articles