Error query SQL (undefined variable)

See my code first


<?php
 
include(dirname(dirname(__FILE__)) . "/pathdefiner.php");
 
include_once (BASEPATH . INC . "logged.php");
 
if (!isLoggedIn()) {
include ("templates/login.html.php");
exit();
}
 
if (isset($_GET['addp'])) {
$title = "Add post";
$desc = "Type down what your post want to be";
$action = "?addpost";
$id = '';
$titlepost = '';
$button = 'Add post';
$editmode = FALSE;
 
include "templates/form.html.php";
die();
}
 
if (isset($_GET['addpost'])) {
if (empty($_POST['post']) || empty($_POST['title'])) {
echo 'Please fill and set the post entry' . '<br>' . '<p><a href=".">Go Back</a></p>';
die();
}
 
include_once (BASEPATH . INC . "connect.php");
 
try {
$sql = "INSERT INTO `posts` SET
`title` = :title,
`posttext` = :post,
`posttime` = :time";
$s = $pdo->prepare($sql);
$s->execute(
array(
':title' => $_POST['title'],
':post' => $_POST['post'],
':time' => time()
)
);
}
catch(PDOException $e) {
die("Error adding post. Error: " . $e->getMessage());
}
 
header('Location: .');
die();
}
 
include "templates/admin.html.php";
 
include "templates/logout.inc.html.php";

After executed, it generates an error that the $pdo variable is not set…

But, if i change the include_once to include, it will work well…
And, if i change the $pdo variable in my connect.php to a global scope, it will work well too…

Can you tell me why this happens?
Why if i change include_once to include, it will work ?
Why if i set the $pdo variable to global, it will work ?

Which’s better ? Change include_once to include or set the global scope for $pdo ?

This is my connect.php


<?php
 
require_once (dirname(__FILE__) . '/config.php');
 
define("DSN", 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME);
 
try
{
$pdo = new PDO(DSN, DB_UN, DB_PW);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
echo DB_PW . "<br>";
die('Error: ' . $e->getMessage());
}

I’m gonna guess because the include_once detected that you loaded that module somewhere else, probably in a function, thus making the $pdo local to that function only.