user962206
user962206

Reputation: 16147

Are PHP script read from Top to bottom?

Are PHP scripts reads from top to bottom? like HTML? because in this code

<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>
<?php 

    if(isset($_GET['subj']))
    {
        $sel_subj = get_subject_by_id($_GET['subj']);
        $sel_page = NULL;

    }else if(isset($_GET['page']))
    {
        $sel_subj = NULL;
        $sel_page = get_page_by_id($_GET['page']);
    }else
    {
        $sel_subj = NULL;
        $sel_page = NULL;
    }


?>
<?php include("includes/header.inc.php"); ?>
<table id="structure">
    <tr>
        <td id="navigation">
            <ul class = "subjects">
                <?php

                    $subject_set = get_all_subjects();

                    while($subject = mysql_fetch_array($subject_set))
                    {
                        echo "<li";
                        if($subject['id'] == $sel_subj['id']) {echo " class =\"selected\"";}
                        echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) . 
                        "\">{$subject["menu_name"]}</a></li>";

                        echo "<ul class = 'pages'>";

                        $page_set = get_pages_for_subject($subject['id']);

                        while($page = mysql_fetch_array($page_set))
                        {
                            echo "<li"; 
                            if($page['id'] == $sel_page['id']){echo " class = \"selected\"";}
                            echo"><a href=\"content.php?page=" . urlencode($page["id"]) .
                            "\">{$page["menu_name"]}</a></li>";
                        }
                        echo "</ul>";
                    }
                ?>
                </ul>
        </td>
        <td id="page">
                <?php if(isset($sel_subj)){?>
                    <h2><?php echo "{$sel_subj['menu_name']}";?></h2>
                <?php } ?>  
                <?php if(isset($sel_page)){?>
                    <h2><?php echo "{$sel_page['menu_name']}"?> </h2>
                <?php }?>
        </td>
    </tr>
</table>
<?php require("includes/footer.inc.php"); ?>

specifically this part

if(isset($_GET['subj']))
        {
            $sel_subj = get_subject_by_id($_GET['subj']);
            $sel_page = NULL;

        }else if(isset($_GET['page']))
        {
            $sel_subj = NULL;
            $sel_page = get_page_by_id($_GET['page']);
        }else
        {
            $sel_subj = NULL;
            $sel_page = NULL;
        }

How is this if-else block being called if it's on top of the page?

Upvotes: 0

Views: 4724

Answers (3)

Matthew
Matthew

Reputation: 48304

It is run top-to-bottom one time per page view. On the initial view, assuming the URL has no parameters, then neither $_GET['subj'] or $_GET['page'] will be set.

If the link pointing back to the same page is clicked, then the entire PHP file will be reprocessed. If that link contained subj or page in the URL as a query variable, then the corresponding if block will be executed and the page will be altered accordingly.

Think of the PHP server as dynamically creating some HTML file that is sent to the web browser. Once it is sent, the server is done, and the PHP code is "gone." The only way to run more PHP code is to request a new page, where the process starts over.

(Even AJAX follows the same principles, although generally then you are dealing with partial data requests as opposed to full page views.)

Upvotes: 2

TylerH4
TylerH4

Reputation: 473

PHP scripts are executed from top-to-bottom. What exactly is your problem?

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 376012

Yes, scripts are run from top to bottom. I don't understand why you think that if-else block is any different? Those if clauses are run to set the $sel_subj and $sel_page variables before the rest of the page is executed and output.

Upvotes: 2

Related Questions