php - If Statement Inside a Function Not working -
similar questions have been posted have not been able find answer.
this if statement works correctly it's self.
$subject_id_top = "case studies"; if($subject_id_top == "case studies") {echo "active";}
but when inserted if statement inside function not work.
<style> .active { color:#f00; } </style> <?php $subject_id_top = "case studies"; function menu_active() { if($subject_id_top == "case studies") {echo "active";} } <a class="<?php menu_active(); ?>" href="#">case studies</a> ?>
this seems basic issue life of me havent been able figure out. community great. in advance.
because $subject_id_top
inside function refers $subject_id_top
outside scope of function (the global scope), have 'import' it, speak, statement:
global $subject_id_top;
you're function should become:
function menu_active() { global $subject_id_top; if($subject_id_top == "case studies") {echo "active";} }
however, relying on information outside scopes, in way, not preferable. refactor function accept argument circumvent behavior, so, instance:
function menu_active( $subject_id_top ) { if($subject_id_top == "case studies") {echo "active";} }
and call function $subject_id_top
, global scope, argument:
<?php menu_active( $subject_id_top ); ?>
Comments
Post a Comment