PHP BASICS


Practice 1. Practical application of Constants

and Variables

Calculate your birth year dynamically
Today's Date:

21 April, 2026

My Name:

sirCharles

My Favourite Colour:

blue

My Age:

54

Procedure


1.Open text editor

2.Create a new File / script (Right Click your working folder > New File > Name of file.php > Input all HTML tags)

3.Enter the following PHP codes before the HTML section

- Define a Constant

define("TITLE","PHP BASICS");

- Add this code to the title tag to echo out the constant as title for the page

< title>< ?php echo TITLE ?>< /title>

- Define your variables

< ?php
$myname = "Charles";
$favcolour = "blue";
$birthyear = "1972";

When using the date() method, PHP has to know the timezone of where the server resides, in order to output the correct hour and date for that geographical location. The date_default_timezone_set() method takes a string that locates the server. The list of supported timezones can be found at http://php.net/manual/en/timezones.php

date_default_timezone_set('Canada/Mountain');

$today = date('F j, Y');  F for full month e.g.January not 01, j for day and Y for full year.

$thisyear = date('Y');

- Use PHP to calculate the difference between your birth year and this year to show your age dynamically

$myage = ($thisyear - $birthyear);

- Display the results on screen

echo $myage;

?>