Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Need help in PHP Switch Statement?

While I am doing coding in Java, we can use two output to give actual number in switch like:

In Java:

case 3: case 4: case 5: echo "They all are same"; echo "But this is the actual value is " + x; break; // Output - But the actual value is 3

In PHP:

case 3:case 4:case 5: echo "They all are same"; echo "But the actual value " + $x; break; // Output - "3"

In php, it is showing me "They all are same 3" Why it is not showing the second echo output like "But the actual value is 3"

Full Code

<?php

$x = 3;

switch ($x){ case 1: echo "This is 1"; break;

case 2:
    echo "This is 2";
    break;

case 3:case 4:case 5:
    echo "They all are same ";
    echo "But this is the actual value is " + $x;
    break;

default:
    echo "None of the value is correct";
    break;

}

1 Answer

In Java you concatenate with a " + ", in PHP you use a dot " . ".

<?php
  $x = 3;
  switch ($x){ 

    case 1: 
      echo "This is 1"; 
      break;

    case 2:
      echo "This is 2";
      break;

    case 3:
    case 4:
    case 5:
      echo "They all are same ";
      echo "But this is the actual value is " . $x;
      break;

    default:
      echo "None of the value is correct";
      break;
  }
?>