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

Control Flow: Switch Forum

View Course

463 points
3db29dff2a8f7f8829c1af861906bdc9?s=140&d=retro
Submitted by
Yolanda99
about 3 years ago

switch case use double quote or single quoto?

  switch ($fruit) {
    case "Apple":
        echo "Yummy.";
        break;
   case 'banana':
        echo "Not bad";
   default:
        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

permalink

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
56ec11ba1ec4b00bc400069e_656519138
Submitted by
Gaurang Tandon
about 3 years ago

3 Comments

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

sounds like working in unix bash...

557ede39d3292f7a0000006a_306960922 wolfie almost 3 years ago

Found the answer useful. Thankyou.

7519a67f66b60c9bb1e3be005da93c9c?s=140&d=retro AlexBunt about 2 years ago

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


0 votes

permalink

<!DOCTYPE html>





<?php
switch ($redapple) {
?> case 'redapple'
echo "you chose red apply";
break;
case 'banana'
echo " you chose da banana";
break;
case 'Strawberry'
echo "you have a sweet tooth";
break;
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
Picture
Submitted by
Jonathan Potgieter
about 2 years ago

2 Comments

Picture Jonathan Potgieter about 2 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 almost 2 years ago

You didnt declare a variable $redapple

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


0 votes

permalink

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
4f37f79550a58d36a77b935276e45f6a?s=140&d=retro
Submitted by
typomaniac101
over 1 year ago