Question

I have made a simple weather condition analyser that analyses the weather condition based on a certain website.

My code is:

<?php

$V = "Partly cloudy possible thunderstorms with rain";

function getMyString($SentenceSrc)
{
    if (strpos($SentenceSrc,'cloudy') AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Cloudy with rainshowers or thunderstorms";
    elseif (strpos($SentenceSrc,'rain') OR (strpos($SentenceSrc,'Rain')) AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";
    elseif (strpos($SentenceSrc,'Sun') OR strpos($SentenceSrc,'sun'))
        $SentenceVariable = "Sunny";
    elseif (strpos($SentenceSrc,'Stormy'))
        $SentenceVariable = "Stormy";
    else
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";
}

$y = getMyString($V);

echo 'hi'.$y;

?>

I wish my output to be:

hiCloudy with rainshowers or thunderstorms

But it is only displaying hi. What is wrong?

Was it helpful?

Solution

You are not returning anything from your function.

function getMyString($SentenceSrc)
{
    if (strpos($SentenceSrc,'cloudy') AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Cloudy with rainshowers or thunderstorms";
    else if (strpos($SentenceSrc,'rain') OR (strpos($SentenceSrc,'Rain')) AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";
    else if (strpos($SentenceSrc,'Sun') OR strpos($SentenceSrc,'sun'))
        $SentenceVariable = "Sunny";
    else if (strpos($SentenceSrc,'Stormy'))
        $SentenceVariable = "Stormy";
    else
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";

    return $SentenceVariable;
}

Working Code.

OTHER TIPS

You forgot to return the value from your function:

function getMyString($SentenceSrc)
{
    if (strpos($SentenceSrc,'cloudy') AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Cloudy with rainshowers or thunderstorms";
    elseif (strpos($SentenceSrc,'rain') OR (strpos($SentenceSrc,'Rain')) AND (strpos($SentenceSrc,'thunderstorm') OR strpos($SentenceSrc,'T-storm')))
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";
    elseif (strpos($SentenceSrc,'Sun') OR strpos($SentenceSrc,'sun'))
        $SentenceVariable = "Sunny";
    elseif (strpos($SentenceSrc,'Stormy'))
        $SentenceVariable = "Stormy";
    else
        $SentenceVariable = "Partly cloudy with brief rainshowers or thunderstorms";

    return $SentenceVariable;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top