PHP - variable scope inside if statement and while loop -


i know if statement gives result boolean.

     <?php              if (isset($_get['subj'])) {             $sel_subj = $_get['subj'];             $sel_page = "";      ?> 

can use $sel_subj or $sel_page outside if statement ? second question in case of while loop ? can use variable outside or considered in local scope ?

while ($page = mysql_fetch_array($page_set)) {                 echo "<li";                 if ($page["id"] == $sel_page) { echo " class=\"selected\""; }                 echo "><a href=\"content.php?page=" . urlencode($page["id"]) .                 "\">{$page["menu_name"]}</a></li>";         } 

basically yes, variables defined inside if or while available in scope if or while exists in (as defined in conditional though might not have been set receive undefined warning)

so

    function foo(){         $i=0         while($i==0){            $i=1;            $a=1;         }          echo $a;         //$a available here although might undefined condition may not have been met      }     echo $a //$a not available here 

you should ideally declare variable first.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -