Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP switch, why doesn't this work?

I have a strange problem that I can't seem to solve. I've quite a complicated bit of code going on, but I've 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, his returns meta_title as incorrect eventhough its clearly set in the array. It's almost as if its ignoring the case and just dropping down.

I've set up an example at: http://codepad.org/mQH9Kf1L

Thanks in advance!

UPDATE

It might make more sense to see where I'm using this. See the following: http://codepad.org/WnxBp8Nt (line 43 onwards)

Just out of interest, I changed I added a quick microtimer and tested this version and a version written with seperate ifs. The if version came out a little slower.

like image 264
jleck Avatar asked May 05 '26 12:05

jleck


2 Answers

The reason it's not doing what you want is because if case 1 is true, cases 2 & 3 trigger automatically (and if case 2 is true, case 3 always fire). This is not what switch is for. You really just need 3 separate if clauses:

<?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);
like image 129
Cal Avatar answered May 07 '26 03:05

Cal


Quoting from the PHP Documentation for Switch:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements 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 a case's statement list, PHP will go on executing the statements of the following case.

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);
like image 36
Chibueze Opata Avatar answered May 07 '26 01:05

Chibueze Opata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!