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
Carl Youngman
2,138 PointsHelp with strpos
Hello, I'm trying to check for words within a string for validation. I think I have to use strpos but it just returns false every time. The user is asked to type a simple line of php into a text box <?php echo 'hello world'; ?>. I would like to give 5 of 6 different error message if they miss type.
<?php
if (empty($_POST) === false){
$errors = array();
// User has been asked to type '<?php echo 'hello world' ?/>';
$code = (strlen(trim($_POST["code"])));
if (empty($code) === true) {
$errors[] = 'Please Enter Some Code';
} else {
if (strpos($code, '<?php') == false){
$errors[] = 'Remember to Open your php tags.';
}
if (strpos($code, 'echo') == false ){
$errors[] = 'Make use of the echo Function';
}
if (strpos($code, ';') == false ){
$errors[] = 'Make sure you have included your line terminator';
}
if (strpos($code, '?>') == false ){
$errors[] = 'Make sure to include your closing php tags';
}
else{
$errors[] = 'Code fine';
}
}
if (empty($errors) === true){
$errors[] = 'code working';
}
}
?>
5 Answers
Sean Gaffney
Front End Web Development Techdegree Student 2,560 PointsHey Carl,
The problem is you're using a double equals (==) instead of triple equals (===). In PHP, == means "equality", whereas === means "identical". So in this code:
<?php
if (strpos($code, '<?php') == false){
$errors[] = 'Remember to Open your php tags.';
}
?>
Since <?php is at the beginning of the string, the strpos returns 0, and in PHP the statement 0 == false evaluates to true, which sets the error message. If you use a triple equals, it will ask if 0 is identical to false, which it is not, and you'll get the results you're looking for.
Carl Youngman
2,138 PointsMaybe strpos isnt the right thing to use. I wanted to check if '<?php' is included any where within the text area.
Also triple equals is giving me the same results. I get the error message not matter what.
I dont know what I should be researching to make this work :/
Randy Hoyt
Treehouse Guest TeacherYeah, strpos is the right function, though you might want to use stripos if you aren't worried about case.
It looks like your very first check is wrong. Shouldn't you be checking if $POST is _NOT empty?
if (empty($_POST) !== false){
Does that fix it?
Carl Youngman
2,138 PointsNope, Still not worrying correctly. Everything is working apart from when I do enter the correct text I still get the error messages. Also, if I change it to !== false I get undefined function error =(
Randy Hoyt
Treehouse Guest TeacherYou definitely have to have that first line as this:
if (empty($_POST) !== false){
That code is checking if the $_POST variable is empty. If it's NOT empty, then you want to run your validation. If it is empty, then you don't have a POST submission.
Looking further down, I see that you are misusing the strlen function. That returns the length of a string. So let's say someone typed in <?php echo 'hello world' ?> like they were supposed to, then this line ...
$code = (strlen(trim($_POST["code"])));
... would assign $code a value of 27. All the rest of your checks are looking at this number instead of the code, so it fails.
Does that help?