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.
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
switchis for. You really just need 3 separateifclauses: