newbtophp Posted October 9, 2009 Share Posted October 9, 2009 I have alot of if statements to the variable $file. How can i only echo 1 if, instead of more (if more then one if statement matches $file)? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/ Share on other sites More sharing options...
lemmin Posted October 9, 2009 Share Posted October 9, 2009 What do you mean? Can you show some code? Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933888 Share on other sites More sharing options...
newbtophp Posted October 9, 2009 Author Share Posted October 9, 2009 <?php $file = "hello, I have a pet goat, i know its random, but who cares?"; if(preg_match('/goat/', $file)) { echo "it contains goat fool!"; } if(preg_match('/hello/', $file)) { echo "it contains hello!"; } if(preg_match('/random/', $file)) { echo "it contains random!"; } ?> It would echo all of them, how would i only allow it to echo the first match (like in this case 'hello') Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933892 Share on other sites More sharing options...
lemmin Posted October 9, 2009 Share Posted October 9, 2009 You could change the echos to die()'s, but this might not be practical if you have more code that will execute: $file = "hello, I have a pet goat, i know its random, but who cares?"; if(preg_match('/goat'/, $file)) { die("it contains goat fool!"); } if(preg_match('/hello'/, $file)) { die("it contains hello!"); } if(preg_match('/random'/, $file)) { die("it contains random!"); } If you need to execute more code, you can make it into a function for parsing the file: function parseFile($file) { if(preg_match('/goat'/, $file)) { echo "it contains goat fool!"; return; } if(preg_match('/hello'/, $file)) { echo "it contains hello!"; return; } if(preg_match('/random'/, $file)) { echo "it contains random!"; return; } } Then just call that function somewhere in your code. Or you could use else if: <?php $file = "hello, I have a pet goat, i know its random, but who cares?"; if(preg_match('/goat/', $file)) { echo "it contains goat fool!"; } else if(preg_match('/hello/', $file)) { echo "it contains hello!"; } else if(preg_match('/random/', $file)) { echo "it contains random!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933893 Share on other sites More sharing options...
Alex Posted October 9, 2009 Share Posted October 9, 2009 You're not matching any patterns, no need for preg_match(): <?php $file = "hello, I have a pet goat, i know its random, but who cares?"; $words = Array('goat', 'hello', 'random'); foreach($words as $word) if(strpos($file, $word) !== false) echo 'Found ' . $word . '<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933899 Share on other sites More sharing options...
Alex Posted October 9, 2009 Share Posted October 9, 2009 Oh sorry, I didn't realize that you wanted only one to echo.. Just add a break to my previous example (I can't edit it): <?php $file = "hello, I have a pet goat, i know its random, but who cares?"; $words = Array('goat', 'hello', 'random'); foreach($words as $word) if(strpos($file, $word) !== false) { echo 'Found ' . $word . '<br />'; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933906 Share on other sites More sharing options...
newbtophp Posted October 9, 2009 Author Share Posted October 9, 2009 Thanks both of you, both are great! Quote Link to comment https://forums.phpfreaks.com/topic/177116-solved-only-echo-1-if/#findComment-933920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.