1
<?php
function table() {
   ot();
   for($x=0; $x<$rows; $x++) {  
   table_row($x);
}
   ct();
}
?>

Notice: Undefined variable: rows in .../scratch.php on line 12

Hi,

This function is returning an error because $rows is not defined locally. I define the variable $rows in another php script, which is referenced via "includes('includes.php')" at the top of this script file.

How do I pass or "reference" the variable $rows into this function? As you can tell, I'm still learning PHP and any help is greatly appreciated!

thx,

2 Answers 2

5

Define your function like this:

function table($rows) {
   ot();
   for($x=0; $x<$rows; $x++) {  
   table_row($x);
}

And then call it like this:

table($rows);

Where the $rows variable is defined in your calling script.

The other option would be to make $rows a global variable, in which case you can do:

function table() {
    global $rows;
    //etc
}

However, global variables should be avoided when possible, so I'd still recommend the first method.

Sign up to request clarification or add additional context in comments.

Comments

2

if you want to use global variable withing the function you need to explicity declare it.

<?php
function table() {
    global $rows;
    for($x = 0; $x < $rows; $x++) {
        table_row($x);
    }
}

In most cases it is not a good idea to rely on globals and you should consider passing $rows as a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.