AJAX,PHP变量和我

I'm learning how to use AJAX and I made 4 files:

header.php
page1.php
page2.php
footer.php

My header contains like body, html, head and things. While my footer, </body>...
I need to make a navbar that calls page1.php trough AJAX, but what if user access page1.php directly, what happens to header and footer ;-;?
I tried to make this two things in PHP:

header.php:

<? $a = 1 ?>

page1.php:

<? if ($a != 1){include 'includes/header.php';} ?>

But page1 keeps calling header even when bloody $a IS EQUAL 1!!!!!!

I also made this in page1.php:

<? echo $a ?>

And guess what? Yes... it returns 1...

AJAX code in header.php:

$(function() {
            $('nav a').click(function(e) {

            e.preventDefault();
            var href = $(this).attr('href');

            $('#content').html('Downloading...');

            $.ajax({
                url: href,
                type: 'GET',
                error: function(){
                    // always good to have an error handler with AJAX
                },
                success: function(data){
                    $('#content').html(data);
                }
            });

                // HISTORY.PUSHSTATE
                history.pushState('', 'New URL: '+href, href);
                // e.preventDefault();


            });

HALP!

If page1.php is loaded with AJAX it can not evaluate the $a variable declared in the header.php because all PHP code in page1.php is executed serverside before the response is returned to the header page. But you can however pass a variable as a parameter along with the request in the url

var href = $(this).attr('href')+'?a='+'<?php echo $a; ?>';

This will pass a parameter with value of $a with the url like this: //page1.php?a=1

In your page1.php you can then check if this variable is NOT passed (meaning header.php didn't request the page):

if (!isset($_GET['a'])){
    include 'includes/header.php';
}