lamboFOD Posted June 10, 2015 Share Posted June 10, 2015 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 More sharing options...
requinix Posted June 10, 2015 Share Posted June 10, 2015 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... Link to comment https://forums.phpfreaks.com/topic/296733-switch-case-wont-work-on-read/#findComment-1513581 Share on other sites More sharing options...
ginerjm Posted June 10, 2015 Share Posted June 10, 2015 What do you mean by ' isn't currently working '? Could it be because you aren't using it correctly? Check the manuals for proper examples. Link to comment https://forums.phpfreaks.com/topic/296733-switch-case-wont-work-on-read/#findComment-1513582 Share on other sites More sharing options...
grissom Posted June 10, 2015 Share Posted June 10, 2015 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); ?> Link to comment https://forums.phpfreaks.com/topic/296733-switch-case-wont-work-on-read/#findComment-1513601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.