Jump to content

PhP -> Echo Question


Massacres

Recommended Posts

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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 ^_^
Link to comment
Share on other sites

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.