如果通过函数调用PHP代码的行为不一样

The same code used as below works fine, but when you un-comment the function and try to call the function, it does not behave the same? Am trying to standardize the CRUD using functions, but running into issues.

  1. The print_r does not display the output when called inside a function but works when called as is outside of a function
  2. the return value does not return when called via a function
  3. I would like to get the return values via the function and decide what to do next.

Appreciate if you could help me out. Thanks

<?php
    //function verifyemail($p_email) {
        echo "insde function - " . $p_email;
        try {
            //      $p_email = "r@gmail.com";
            include 'db.php';
            connectDB('msme_db',1) ;
            $sql = "select count(*) as p_exists from    msme_users where user_email = '$p_email' ;" ;
            echo $sql ;
            $result = $__conn->prepare($sql);
            $result->execute();
            $result->setFetchMode(PDO::FETCH_ASSOC);
            $row = $result->fetch() ;
            //print_r ($row);
            if ($row)
            {
                $i = $row['p_exists'] ;
                return $row !== false ? $row : 'x';
            } 
       } catch (PDOException $e) {
            echo  " in catch" ;
            die("Error occurred:" . $e->getMessage());
       }
//} // End of Function

//$email = "r@gmail.com";
//echo sprintf('Email %s is %s', $mail, verifyemail($email)) ;
print_r($row) ;
?>

Problems I see: 1. you have return command in if statement

if ($row)
{
    $i = $row['p_exists'] ;
    return $row !== false ? $row : 'x';
}

so if is true then you return from function and if is not true then you don't
check this by putting echo inside and see if you see anything

if ($row)
{
    echo 'IF Statemen in line: ' . __LINE__ . '<br>' . PHP_EOL;
    $i = $row['p_exists'] ;
    return $row !== false ? $row : 'x';
}

you should rewrite the code so you always return form the function and use if statement only to assign value to $i or $row
ie like that:

function verifyemail($p_email) {
    echo "insde function - " . $p_email;
    try {
        //      $p_email = "r@gmail.com";
        include 'db.php';
        connectDB('msme_db',1) ;
        $sql = "select count(*) as p_exists from    msme_users where user_email = '$p_email' ;" ;
        echo $sql ;
        $result = $__conn->prepare($sql);
        $result->execute();
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $row = $result->fetch() ;
        //print_r ($row);
        if ($row)
        {
            $i = $row['p_exists'] ; //<== where do you use $i?
            $row !== false ? $row : 'x';
        } 
   } catch (PDOException $e) {
        echo  " in catch" ;
        die("Error occurred:" . $e->getMessage());
   }
   return $row; //<== here you rentrun from the function
} 
// End of Function
// ^ here is place for the comment, not in the line where } is

//$email = "r@gmail.com";
//echo sprintf('Email %s is %s', $mail, verifyemail($email)) ;
print_r($row) ;
?>
  1. If you use return statement without the function body so in the global code scope you end the php script execution
    so the code

    print_r($row) ;
    

is never executed

to summarize - put echo statements

echo 'line: ' . __LINE__ . '<br>' . PHP_EOL;

into if statement and other places and check numbers with the code and you'll probably see what you havn't seen where your execution flows where not.
and move return statement outside if statement, perfectly at the end of the function.