While PHP is weakly types, which means you don't have to specify what type a certain variable will hold, you still need to realize that there are indeed types involved.
Also, in strings enclosed in single quotes, PHP will not substitute parts of the string matching variable names for their values. No parsing at all is done in this case (other than for some escape sequences).
This is the string: $mes,$mes2
That is, 10 characters consisting of the exact same thing you see in the code.
'$mes,$mes2'
This however is the string: 01,2
"$mes,$mes2"
This is a number, and it is the number 1
01
This is a string, '01', and when cast to integer it has the value 1
'01'
So while it may appear safe to write 01 and '01', there is one major difference. When '01' is cast to integer, it is treated as a decimal number. The integer 01 is considered an octal number (any integer starting with 0 is), which has the same value as the decimal number 1.
Octal numbers consist of the digits 0-7, and what PHP does if it encounters something else is to disregard the rest of the number.
So 01 through 07 equals 1 through 7. 08 however, becomes 0.
Also, strings that starts with non digit characters become 0. This means you can do things like
if ('a' == 0)
echo "'a' == 0";
Now, let's have a look at your code again
# The string '$mes1,$mes2' is cast to int, and becomes 0.
switch('$mes,$mes2') {
# Same as writing: case 1:
case 01:
//...
# same as writing: case 7:
case 07:
// ...
# same as writing: case 0: (since 08 is octal and the 8 will be disregarded)
case 08:
}
This explains why you get 'August'. Also note that you'd get this no matter what day of the year this code is run. However, the code would not do what (I'm guessing) you want it to do, even if you had
switch("$mes,$mes2")
The above becomes the string '01,2', which is case to integer and becomes 1, thus matching case 01. Then your output would tell you it's no january, and the next month is january.
Also, note that above, $mes is the string '01', while $mes2 is the integer 2. Else the string would have become '01,02' or the string '1,2'.
The reason is this
$mes = date('m'); // function returns the string '01'
// the + operator doesn't work with strings, so $mes is cast to int
// which gives 1 + 1 = 2. $mes2 gets the value 2 and type integer.
$mes2 = $mes + 1;
If I'm guessing correctly, you'd like switch($mes,$mes2) to first perform the switch for $mes, and then for $mes2. This will not work. However, what I'd do is skip the $mes2 variable alltogether and set up your switch like this
switch($mes) {
# Drop the 0...
case 1:
$mes = 'Enero';
$mes2 = 'Febrero';
break;
// ...
case 12:
$mes = 'Diciembre';
$mes2 = 'Enero';
break;
}