0

as the title said, i've this block of code and i want to know if i can write a variable that takes the name of another one similar to it but outside the loop.

is that makes a problem ?!

<?php 
   $query = "SELECT * FROM subjects";
   $subjects_set = mysql_query($query);
   errors_of($subjects_set);

    while ($db_subjects_rows = mysql_fetch_array($subjects_set)){
           echo "<li>{$db_subjects_rows["menu_name"]}</li>";

           $pages_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$db_subjects_rows["id"]}");


        errors_of($pages_set);
        echo "<ul class=\"pages\">";
        while ($db_pages_rows = mysql_fetch_array($pages_set)){
        echo "<li>{$db_pages_rows["menu_name"]}</li>";
        }
            echo "</ul>";                       
    }

?>
2

2 Answers 2

2

Using local variables inside a loop does not create a problem, if thats what you are asking, you should read about variable scopes

The function has a scope, the class has a scope and there is a global scope

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

5 Comments

A.Shabaan but what i'm asking for the variable inside the loop. the similarity between it and the one outside could make an error if i use the outside one in a function for example ?!
What is the name of the variable you are talking about ?
i think i get it permanently. but if u donn mind i've another question :) i've this function that i want to pass a value on it "function get_all_subjects ($all_subjects){ $query = "SELECT * FROM subjects"; $subjects_set = mysql_query($query); errors_of($subjects_set); }" and when i recall it in the page this error appear "Notice: Undefined variable: all_subjects in C:\wamp\www\test_of_test\content.php" although that in the content.php page i put the function as it get_all_subjects($all_subjects); why its undefined ?!
The variables inside the function are read only inside the function to change the value of the variable passed in the global scope you should pass it by reference, hence function get_all_subjects(& $all_subjects){// do some coding}
i sent you a message on your web page please find it out and send me back :)
1

All the variables that is used outside the loop can be accessed inside the loop.Refer this manual to know more about the PHP Variable Scope.

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.