In PDO examples I often find the acronyms $dbh
and $sth
used. I suppose $dbh
stands for "database handle" - correct? What about $sth
? "Statement handle"?
Are there good reasons to use the above instead of $db_connection
and $query
(or other as appropriate)?
$DBH = null; You can get more information on database-specific options and/or connection strings for other databases from PHP.net.
PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API (Application Programming Interface).
Introduction ¶ PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL databases.
PDO::PARAM_STR (int) Represents the SQL CHAR, VARCHAR, or other string data type. PDO::PARAM_STR_NATL (int) Flag to denote a string uses the national character set.
$dbh = "Database Handle"
$sth = "Statement Handle"
I prefer the longer forms, as they are more descriptive. It is often helpful to future maintainers if you are explicit, even when using common acronyms and abbreviations.
In the past, when hard drive capacity, memory and bandwidth were scarcer, the abbreviations may have made sense. Today there is (arguably) greater value in producing readable, maintainable code.
Your intuition was correct as to what those variable names stand for, but that isn't to say that every use of PDO uses those variable names, just the particular code you're looking at.
Variable names are never significant to the correct execution of a function that you pass them to. I've seen people use obscenities for variable names in their entire project... it just doesn't matter to the functions. For your own sake, and the sake of any future developers who work with you, variable names should be concise and clear. Abbreviations should be avoided when you can help it, since it may not be clear to someone else that $hndlr
is how you abbreviate "handler" - as this very question demonstrates, abbreviations are seldom intuitive.
It is more important that you develop or choose a coding standard and stick with it. If your variables are all lower_case_with_underscores, stick to that regardless of what you see people doing in other code outside of your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With