Jump to content

Switch case wont work on read


lamboFOD

Recommended Posts

my switch case isnt currently working, when the variable it uses is bought over from a text file. 

<?php 
				$file = fopen("Update/Location.txt", "r") or die("Unable to find file");
				$string = fread($file,filesize("Update/Location.txt"));
				echo $string . "<br>";
				
				
				switch ($string) {
   					case "Home":
						echo "I am currently " . $string . ", feel free to contact me whichever way is best for you.<br>";
   					case "Work":
    				case "University":
        				echo "I am currently " . $string . ", the best way to contact me is through email or text message<br>";
        				break;
					default:
						echo "i am currently unreachable <br>";
						break;
				}



fclose($file);
?>

for further reference, please see my basic website: http://ryanclambert.worcestercomputing.com

 

cheers

Location.txt

locread.php

Link to comment
https://forums.phpfreaks.com/topic/296733-switch-case-wont-work-on-read/
Share on other sites

There's a newline at the end of the file. How you deal with that is up to you: remove the newline, trim() it off, use a different function than fread() which stops at newlines, use file_get_contents() and avoid the whole file opening thing though you'll still have the newline...

From what I can see, it also looks like you need another break in your code

 

<?php
                $file = fopen("Update/Location.txt", "r") or die("Unable to find file");
                $string = fread($file,filesize("Update/Location.txt"));
                echo $string . "<br>";
                
                
                switch ($string) {
                    case "Home":
                        echo "I am currently " . $string . ", feel free to contact me whichever way is best for you.<br>";
break;
                    case "Work":
                case "University":
                echo "I am currently " . $string . ", the best way to contact me is through email or text message<br>";
                break;
                    default:
                        echo "i am currently unreachable <br>";
                        break;
                }



fclose($file);
?>

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.