I have written a long script that does exactly what I need, but now I am find out the usefulness of functions so I can use them in other scripts.
My script works perfectly the way I originally wrote it, and also works perfectly with the way I implemented the function I created.
As I am brand new to writing functions and I have never seen one implemented the way I have done, I don’t know if it is good form or not.
Original Way
foreach($get_prefixes as $prefix) {
if($prefix['type'] == 'pre') {
$sql = "SHOW COLUMNS FROM `$prefix[table]` LIKE 'sku_%' ";
$sku_cols = $objDb->query($sql);
foreach($sku_cols as $col) {
echo $col['Field'] . '<br>';
}
}
}
And then there is the way I did it with the created function. Again it works perfectly, but is there is a "proper" way to do this?
function getSku_($table) {
global $objDb;
$sql = "SHOW COLUMNS FROM `$table` LIKE 'sku_%' ";
$sku_cols = $objDb->query($sql);
return $sku_cols;
}
foreach($get_prefixes as $prefix) {
if($prefix['type'] == 'pre') {
foreach(getSku_($prefix['table']) as $col) {
echo $col['Field'] . '<br>';
}
}
}
I don’t see anything wrong with the function, except for its
naming. It’s not incorrect, just that I understand its convention is to follow naming asabc_pqrorabcPqror_abcPqr (for private functions).Edit: I also don’t see
$get_prefixesdefined anywhere, before using in the foreach, in the function. Where is it coming from?If the functions differ in just the query,
"sku"and"sku "then you don’t even need two functions, just use anORin the query (preferred) or maybe two queries, if you want the two result sets to be separate.