Jump to content

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


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.

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.