Siyam Hossain Siyam Hossain - 1 month ago 8
PHP Question

Getting error: Undefined offset: 1

Its look like everything ok. It shows the result as well but it also shows Undefined offset: 1 error. Please help me with this.

enter image description here

$url = "http://www.test.com";
$pageContent = file_get_contents($url);
$stepA = explode("</title>",$pageContent);
$stepB = explode("<title>",$stepA[0]);
$stepC = $stepB[1];
if($stepC == "Not Found"){
echo $stepC = "NA";
} else{
echo $stepC = "ok";
}

Answer

Add some code like bellow:

$url = "http://www.test.com";
$pageContent = file_get_contents($url);

$stepA = explode("</title>",$pageContent);

if(isset($stepA)) {
    $stepB = explode("<title>",$stepA[0]);

    $stepC = isset($stepB) ? $stepB[1] : null;

    if($stepC == "Not Found"){
        echo $stepC = "NA";
    } else{
        echo $stepC = "ok";
    }
}