Jump to content

Help figuring out when to use "echo", if at all.


galvin

Recommended Posts

I know this is stupid newbie stuff, but I can't think about this the right way.  And this question might very well make NO sense. If so, I apologize in advance.  I'm trying to consolidate code.  I basically have lots of quizzes, all with their own answers, which I compare using AJAX.  I had individual pages for each quizzes answers to do that comparison and everything was working fine.  It would do the following...

<?php
$userinput = strval($_GET['userinput']);
$userinput= trim($userinput);
$userinput = strtolower($userinput);

switch ($userinput) {
     case 'heat':
 case 'miami heat':
          echo 'Miami Heat';
          exit;
     case 'magic':
 case 'orlando magic':
          echo 'Orlando Magic';
          exit;
     case 'jazz':
 case 'utah jazz':
          echo 'Utah Jazz';
          exit;

  	  		  
     default:
          echo 'incorrectguess';
}
?>

 

But now I am storing the answers in a MySQL database (for example, one answer set is in the database as "Miami Heat/heat/miami"...each allowed answer is separated by a "/", with the first answer being the "main" correct answer).  So now, I want to just have ONE "comparison" page and populate the "cases" into that page dynamically, from the database.  This is what I came up with so far, but it's not working...

 

<?php

$userinput = strval($_GET['userinput']);
$userinput= trim($userinput);
$userinput = strtolower($userinput);


if ($_GET['quizid'] == "") {
redirect_to('index.php');
} else {
$quizid=$_GET['quizid'];
}

$sql = "SELECT * FROM answers
WHERE quizid = $quizid";
$result = mysql_query($sql, $connection);
if (!$result) { 
die("Database query failed: " . mysql_error());
} else {

switch ($userinput) {

while ($results = mysql_fetch_array($result)) {
	$answers = explode("/", $results['answer']);
	$n = count($answers);
		for ($i=1; $i < $n; $i++) {
			case '$answers[$i]':
		}
		echo '$answers[0]';
		exit;


			 default:
			echo 'incorrectguess';
				  
}

}
?>

 

 

What I'm having trouble grasping is where I need to use "echo" or NOT use "echo".  For example, where I have "switch ($userinput) {" in the NEW code, does that need to actually be "echo 'switch ($userinput) {';"?

 

I'm trying different combos and failing, so just curious if someone can help me understand what, if anything, needs to be echoed when doing it the 2nd way (dynamic info) rather than the 1st way (static info)

Link to comment
Share on other sites

First off, you don't need to echo switch, however at the end of each case, you need to break;

Not exit.

 

Look at the php documentation on php.net, it will help you a TON. exit kills the whole script, alias of die if you weren't sure.

Also, your default case doesn't have a break or exit, it needs a break.

 

Think of each case as, what happens when the case is this.

 

Whatever happens in between case 'heat' and the next break, is the entire page for when the case is heat.

 

Some resources:

http://php.net/manual/en/control-structures.switch.php

http://www.php.net/manual/en/control-structures.break.php

 

Link to comment
Share on other sites

  • 2 weeks later...

Also, your default case doesn't have a break or exit, it needs a break.

Not true.

A break is only required to prevent the switch rolling onto the next case. As default is at the end, a break is of no use. It's not used in two of the examples at http://php.net/manual/en/control-structures.switch.php

 

A break is needed earlier on, because it prevents the following deliberate behaviour.

switch($var)
{
  case 'headlinedmessage':
    echo 'here is the headline'; //will only say this when $var = headlinedmessage
  case 'message':
    echo 'here is the message'; //will say this when $var is headlinedmessage or message
    break;
}

Link to comment
Share on other sites

... As default is at the end ...

 

The default does NOT have to be at the end of the switch. I sometimes put it at the top of the switch because it more clearly "documents" what that particular switch is doing. The more correct statement would be: "the LAST case (or default) does not require a break".  I have also gotten into the habit of ALWAYS including a break, so if I come back later and add a case at the end of the switch, I don't forget to add a break to the preceding case.

 

 

back to the OP

It looks like you are trying to create a switch dynamically, and I really don't think you can do that. However, the solution, I think, is much simpler:

 

$answers = explode("/", $results['answer']);
if (in_array($userinput, $answers)) {
   echo $answers[0];
} else {
   echo 'incorrectguess';
}

Link to comment
Share on other sites

"the LAST case (or default) does not require a break". 

Eloquently put. I was simply stating that because default was at the end of that example, a break wasn't needed. I agree it's good practice to include it though, for the reasons you've specified.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.