Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Compare a variable with "<br>"

How can i compare a variable with break tag <br> My code is below:

if(strcmp($notes,'br')==0) { echo "No notes are present"; }

1 Answer

I'm not sure exactly sure what you're looking for, as you're not really providing enough information for me to tell, so I'm going to make some assumptions.

What I'm assuming you're trying to do, is tell whether there are any break-tags in the variable $notes. You're using strcmp for this, which is not going to yield a response that you can use in this situation.

Taken from the official PHP documentation, strcmp returns > 0 if string 1 is greater than string 2, returns < 0 if string 2 is greater than string 1 and 0 if string 1 is equal to string 2.

So, in your case, if the variable $notes is greater than "br", which it is if you have more than three characters in the variable, then it will not return 0.

Instead, we should use strpos. Strpos returns false if the string we're looking for (in this case the break-tag) is not found in the string we're looking in (in this case $notes).

Using strpos, the code will look something like this

<?php
if (strpos($notes,'<br>') !== false) {
  echo "Notes are present.";
} else {
  echo "No notes are present.";
}
?>

This returns "Notes are present", if there are any breaks in the variable $notes, and "No notes are present", if no breaks exist.

Is this what you're looking for, or am I making some outlandish assumptions? :-)