0
<?php


$V = "Stormy";
$W = "Heavy thunderstorms";

function getMyString($SentenceSrc)
{
    if ((strpos($SentenceSrc,'Heavy thunderstorms')!== true) OR (strpos($SentenceSrc,'Heavy t-storms')!== true))
     $SentenceVariable = "Rains with gusty winds";

    elseif ((strpos($SentenceSrc,'Sun')!== true) OR (strpos($SentenceSrc,'sun')!== true))
        $SentenceVariable = "Sunny";
    elseif ((strpos($SentenceSrc,'Stormy')!== true))
        $SentenceVariable = "Stormy";
    else
        $SentenceVariable = "Partly cloudy ";

    return $SentenceVariable;
}



echo getMyString($V);
echo getMyString($W);


?>

This is my code. The output should be:

StormyRains with gusty winds

But instead, it only reads the first part of the condition, and returns it True, when it is false.

my getMyString($SentenceSrc) is supposed to find a string within a given string and return a weather condition whenever the given string returns true.

2
  • strpos returns a number (the position) or false (never true), I don't know if comparing strpos()!==true is doing something, try comparing to false Commented Feb 11, 2014 at 9:42
  • Instead of OR, you should try to use || Commented Feb 11, 2014 at 9:42

4 Answers 4

1

I've changed your !== true to > -1

<?php
$V = "Stormy";
$W = "Heavy thunderstorms";

function getMyString($SentenceSrc)
{
    if ((strpos($SentenceSrc,'Heavy thunderstorms') > -1) OR (strpos($SentenceSrc,'Heavy t-storms') > -1))
        $SentenceVariable = "Rains with gusty winds";
    elseif ((strpos($SentenceSrc,'Sun') > -1) OR (strpos($SentenceSrc,'sun') > -1))
        $SentenceVariable = "Sunny";
    elseif ((strpos($SentenceSrc,'Stormy') > -1))
        $SentenceVariable = "Stormy";
    else
        $SentenceVariable = "Partly cloudy ";

    return $SentenceVariable;
}

echo getMyString($V);
echo '<br />';
echo getMyString($W);
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Can I ask for an explanation to this?
+1 for quick answer. i also posted the answer but because you have given first i have deleted my post
@Clary If found something, return the location of the matching phrase/substring and if none, return false. But for some reason, === false is not working on mine so we just look for int greater than -1 because the start of the string is 0. so yeah.
That's because you're checking different things there. If you're checking for > -1 you're checking for a true. If you do a check on ===false you're checking on a false. Execute following code: <?php if (strpos("test", "test") > -1) echo "success"; if (strpos("test", "test") === false) echo "success"; ?> The first one will return success because 'test' is in the string 'test'. The second one will fail because 'test' is in 'test' and therefor will not return false.
0

strpos($a, $b)!==true is always false, because strpos returns an integer when the string is found.

Use strpos($a, $b) === false instead.

1 Comment

For the boolean logic to keep working, you'll have to change the OR to AND also.
0

It is functioning the way you have written it. strpos never returns true. It eigther returns position of the needle if found or false.

So your first condition always correct.

What you need to do is:

if ((strpos($SentenceSrc,'Heavy thunderstorms')=== false) OR (strpos($SentenceSrc,'Heavy t-storms')=== false))

Comments

0

try this

    function getMyString($SentenceSrc)
    {
        if (stristr($SentenceSrc,'Heavy thunderstorms') || stristr($SentenceSrc,'Heavy t-storms')){
            $SentenceVariable = "Rains with gusty winds";
        }
        elseif (stristr($SentenceSrc,'Sun') || stristr($SentenceSrc,'sun')){
            $SentenceVariable = "Sunny";
        }
        elseif (stristr($SentenceSrc,'Stormy')){
            $SentenceVariable = "Stormy";
        }
        else {
            $SentenceVariable = "Partly cloudy ";
        }
        return $SentenceVariable;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.