Click to See Complete Forum and Search --> : Need coding advice.
I need some coding advice.
I have the following code:
<?= editText(7); ?>
<?php
echo $vars["field-7"];
?>
I have several calls similar to the above - the only variable being the number.
What I really want is something like this:
<?php myFunction(7) ?>
But: how do I declare myFunction()...??
I want myFunction to echo the first bit of code: but ONLY echo in the PHP!
I've would have tried bit of research: but am not sure what to read up on!
Thanks.
OM
NogDog
04-25-2008, 10:57 PM
User-Defined Functions (http://www.php.net/manual/en/language.functions.php)
PS: I recommend you avoid the use of the "<?" and "<?=" short tags and always use "<?php" and "<?php echo" respectively, as the short_open_tags option will not even be available at all in PHP 6, so there's no reason to make your scripts dependent on a PHP configuration option now and incompatible with the next major release.
actually... that was just a typo on my part.
in the code i have: it's exactly as u say it is.
any thoughts on my actual problem?
let me know. thanks.
NogDog
04-26-2008, 11:06 AM
From what you've shown us, I don't see a need for a separate function (unless there's more to the story than you've told us). You could do this:
myFunction($num)
{
global $vars;
echo $vars['field-'.$num];
}
But using global makes for tightly coupled code, which is bad in terms of reusability and can lead to difficult to debug errors.
You could get rid of those issues by passing the array in:
function myFunction($array, $name, $num)
{
echo $array[$name.$num];
}
// usage:
$ix = 7;
myFunction($vars, 'field-', $ix);
But now using the function is more work than just typing the command in the first place:
<?php echo $vars['field-'.$ix]; ?>
thanks for the reply.
that was the problem: can i actually use what u have put...
<?php echo $vars['field-'.$ix]; ?> ?
someone else advised me to do this:
function myFunction($num)
{
editText($num);
echo $vars["field-{$num}"];
}
but then: i have the problem of needing to use the global variable.
i didnt realise it was bad programming to us it.
so i guess i would be better off with something like:
function myFunction($array, $num)
{
editText($num);
echo $vars["field-{$num}"];
}
and making the following function call:
myFunction($var, $num) ??
let me know what u think.
please comment on the use of the {} in field-{$num}.
i'm a complete newbie and havent come accross the use of this.
are both correct to use: ur answer and this one?
thanks.
NogDog
04-26-2008, 01:36 PM
Depending on what the source of the array is, the best(?) solution might be to make it a 2-dimension array, e.g.: $var['field'][7] = 'some value';. Then it would not need any function, etc., just:
<?php echo $var['field'][$num]; ?>
If the value is coming from a form, all you need to do is modify the input element's name to use square bracket array notation:
<input type="text" name="field[7]">
Then that value will be in the $_POST or $_GET array as: $_POST['field'][7].
yes... that makes a lot of sense.
i'm basically using some existing code.
not a php guru... but i think i might try to split up as u suggest.
thanks.