Jump to content

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

Edited by lamboFOD
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);
?>
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.