Jump to content

reading from text file and then using a switch case not working together


viperjts10

Recommended Posts

I'm testing out small bits of code for myself, and I'm trying to understand why this isn't coming out how I would imagine it to...

 

I have my function which stores colors in an array from a text file, then I return back a random color from my array like so...

 

	function getColor() {
	$ball_color = explode("\n", file_get_contents('colors.txt')); // Takes file contents and stores them into an array.
	return $ball_color[array_rand($ball_color)];
}

 

And now in the main program, I simply have this:

 

$color = getColor();
switch($color) {
Case "blue":
    	// echo stuff
        break;
    Case "red":
    	// echo stuff
        break;
    etc... 
}

 

For some reason though, my switch statement always reads the default case, making me assume that the words "blue" "red" "green" etc... aren't matching up to what I have set the cases at. When I output what color is actually being read, it displays exactly as it should, so I don't understand why the case "red" isn't opening up when that color is chosen, or same with any of the other colors.

 

Is there some small conflict I'm unaware of in this tiny practice code? Any help is appreciated, thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.