Learning PHP and MySQL

Example 03-07 The default handling of variable scope

<?php

//  define  a  function function  birthday(){
//  Set  age  to  1
$age  =  1;
}

//  Set  age  to  30
$age  =  30;

//  Call  the  function
birthday();

//  Display  the  age echo  $age;

?>

Displays:

30

Posted by on 07/19 at 03:38 PM
  1. I can’t believe that this:

    <?php
    // define a function function birthday(){
    // Set age to 1
    $age = 1;
    }
    // Set age to 30
    $age = 30;
    // Call the function
    birthday();
    // Display the age echo $age;
    ?>

    would actually achieve any useful result.  Maybe you meant

    <?php
    // define a function
    function birthday(){
    // Set age to 1
    $age = 1;
    }
    // Set age to 30
    $age = 30;
    // Call the function
    birthday();
    // Display the age
    echo $age;
    ?>

    Seeing things like this on your web site makes me wish that I hadn’t ordered this book from Amazon - maybe it’s not too late to cancel my order.  Gotta go now.

    Posted by Rex Robards  on  10/12  at  07:55 PM
  2. Page 1 of 1 pages

Next entry: Example 03-08 Using a global variable changes the result

Previous entry: Example 03-05 Using comments to make your code easier to read

<< Back to main