Massacres Posted November 21, 2006 Share Posted November 21, 2006 I just started learning PhP (about half an hour in). Just messing around and making notes. I'm using strpos() to find where a string is in a string. It returns the value of its location, and if it isn't located in it would return no value.You can use it in an if statement as a boolean variable, so I was wondering if I could get it to print out a true/false text output with echo instead of "0, 1, 2, ect.."I also was wondering how I could do a line break in PhP (<br> in html). I made a guess here. C++/C/Turing uses \n to break a line in a string of characters, so I was wondering if there was something similar in PhP?[code]<?php echo strpos('Hello','lo');break;echo 'test' ?>[/code]Note: The 'test' is just to check if its on the next line. Quote Link to comment Share on other sites More sharing options...
fert Posted November 21, 2006 Share Posted November 21, 2006 PHP uses \n as a line break just like C/C++ Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 21, 2006 Share Posted November 21, 2006 when you do like this:echo strpos('Hello','lo');you print the offset where in the word "hello" - "lo" was found, in this case nr 3.to make it print out true or false, you need to make an if-statement, ex:[code=php:0]if (strpos('Hello','lo')) { echo "True";} else { echo "False";}[/code]yes, to make a linebreak you write \n surrounded by " and ", ' and ' wont work. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted November 21, 2006 Share Posted November 21, 2006 In addition to how Nicklas has to print a true/false return you could use this:[code]if (strpos('Hello', 'lo') === TRUE) { echo 'True';} else { echo 'False';}[/code]The way that Nicklas has the if statement, if it returns Zero (as in the string was found at position zero) it would not evaluate to true. This is because PHP will intrepret 0 and FALSE the same, unless you use the === and !== operators to force it to compare value and type. Quote Link to comment Share on other sites More sharing options...
Massacres Posted November 21, 2006 Author Share Posted November 21, 2006 Thank you very much for the help. Answer all my questions and more =D Quote Link to comment Share on other sites More sharing options...
Massacres Posted November 21, 2006 Author Share Posted November 21, 2006 [Afterthought]I was pondering how do do it without an if, and I came up with this;[code]<?php$TF=array("False","True");echo $TF [($Word1===$Word2)];?>[/code]Its kind of basic (programming wise) but I thought someone might be interested.EDIT:It stores False and True in spot 0 and 1 of the array. So when you get (0 or 1) with a boolean comparison, you can get the text version of it ^_^ Quote Link to comment 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.