Php Switch Case String
The switch statement executes line by line actually statement by statement.
Php switch case string. Zunächst einmal wird überhaupt kein code ausgeführt. Home php php switch case statement with case insensitive string comparison php switch case statement with case insensitive string comparison posted by. A switch statement can use an enum ok it are no strings but an integer in disguise you could do something like. Consequently the comparison of string objects in switch statements is case sensitive.
Erst wenn eine case anweisung gefunden wird deren ausdruck zu einem wert evaluiert der dem an switch übergebenen ausdruck entspricht beginnt php. The switch statement compares the string object in its expression with the expressions associated with each case label as if it were using the string equals method. Select enum color undef red orange yellow green blue purple. Admin july 11 2020 leave a comment.
Note that in this example the cases share the same code block and that the default case does not have to be the last case in a switch block however if default is not the last case in the switch block remember to end it with a break. The switch is not made on the string itself but on the numeric value associated to it by the std map. Use break to prevent the code from running into the next case automatically. Only when a case statement is found whose expression evaluates to a value that matches the value of the switch expression does php begin to execute the statements.
Eine switch anweisung wird zeilenweise genauer. In the beginning no code is executed. If there is a match the block of code associated with that case is executed. The value of the expression is then compared with the values for each case in the structure.
In case you have to use a given enumeration where an enumerator with value zero is defined you should call std map find before the switch statement to check if the string value is valid. The switch statement compares the string object in its expression with the expressions associated with each case label as if it were using the string equals method. It is important to understand how the switch statement is executed in order to avoid mistakes. Sometimes you will want different cases to use the same code or fall through to a common default.
Anweisung für anweisung ausgewertet.