PHP: Variables

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Variables

Aims:

Variables in PHP

answer1.php: a script that uses a variable

<body>

    <?php 
        $answer = 'Forty-two.'; 
        echo '<p>What is the answer to Life, the Universe, and Everything?</p>';
        echo '<p>';
        echo $answer; 
        echo '</p>';
    ?>

</body>

Assignment

Initialisation

answer2.php: another script that uses a variable

<body>

    <?php 
        $answer = 'You\'re not going to like it.';
        echo '<p>What is the answer to Life, the Universe, and Everything?</p>';
        echo '<p>';
        echo $answer;
        echo '</p>';
        $answer = 'Forty-two.';
        echo '<p>What is the answer to Life, the Universe, and Everything?</p>';
        echo '<p>';
        echo $answer;
        echo '</p>';
    ?>
 
</body>

This is pointless!

Initialising variables with form data

process.php: a script that greets you by name

<body>

    <?php
        $firstname = $_GET['firstname'];
        $surname = $_GET['surname'];
        
        echo '<p>';
        echo 'Hello ';
        echo $firstname;
        echo ' ';
        echo $surname;
        echo '</p>';
   ?>
 
</body>

Notes on process.php

Class exercise