This forum is now read-only. Please use our new forums at

Control Flow: Switch Forum

View Course

741 points
Submitted by
almost 4 years ago

switch case use double quote or single quoto?

  switch ($fruit) {
    case "Apple":
        echo "Yummy.";
   case 'banana':
        echo "Not bad";
        echo "None";

I find both single and double quote works... so which one should I use? Or, which one is better to use?

26 votes


The single-quoted strings are not parsed at runtime, while the double quoted strings are. The following examples will make it clear:

$money = 567;
echo "I have $money";

Outputs: I have 567. Surprised? Well this is how PHP works. The double-quoted strings, when found, are looked for some hidden, special meaning characters. Now consider this,

$money = 567;
echo 'I have $money'; 

Outputs: I have $money. This is because PHP will not look for any special meaning characters inside of single-quoted strings, and will print it as is. (^)

Single-quoted strings are faster because there isn't any parsing (looking for special characters) that needs to be done.

(^) - The only exception to this are the escape sequences \\ and \'. Example:

echo ' See this \' ';  // See this '
echo ' See this \\ ';  // See this \

The only parsing that takes place in single-quoted strings is looking for \\ and \' and converting them to \ and ' respectively.

Read further on this topic

2570 points
Submitted by
Gaurang Tandon
almost 4 years ago


3db29dff2a8f7f8829c1af861906bdc9?s=140&d=retro Yolanda99 almost 4 years ago

sounds like working in unix bash...

557ede39d3292f7a0000006a_306960922 wolfie over 3 years ago

Found the answer useful. Thankyou.

7519a67f66b60c9bb1e3be005da93c9c?s=140&d=retro AlexBunt almost 3 years ago

Been writing PHP for years and NEVER knew this. Very helpful!

0 votes


<!DOCTYPE html>

switch ($redapple) {
?> case 'redapple'
echo "you chose red apply";
case 'banana'
echo " you chose da banana";
case 'Strawberry'
echo "you have a sweet tooth";
default: ('redapple')

This is my reply for the 6/6 thing where you try out your own PHP falling through option. I'm not sure i have it right, can I acquire some guidance on my error?

27 points
Submitted by
Jonathan Potgieter
almost 3 years ago


Picture Jonathan Potgieter almost 3 years ago

Even just posting the answer is fine, I will then view what is different and fiddle around till i get it.

588b2ac3e1281365f0000133_918398059 Codmakr over 2 years ago

You didnt declare a variable $redapple

$redapple = "redapple"; maybe would give you the result "you chose red apply"

0 votes


I realize I've a ways to go only being 30 minutes into this but it seems as though switch statements would be used, say for validating multiple checkboxes so why the break;

72 points
Submitted by
over 2 years ago