This is pretty basic. Let's say you define a variable in an if statement
if($stmt = $db->prepare($sql)) {
// do stuff
}
Can I access $stmt below the if statement? Or does $stmt need to be defined above the if?
This is pretty basic. Let's say you define a variable in an if statement
if($stmt = $db->prepare($sql)) {
// do stuff
}
Can I access $stmt below the if statement? Or does $stmt need to be defined above the if?
PHP does not have block level scope, only function level scope.
$stmtwill be available anywhere below that if statement (in and out of theif).Something to keep in mind however, if you define new variables inside the
ifblock, will only exist if thatifevaluates totrue.Eg: