Variable declarations specify the type and name of a variable before it can be used in a program.
The memory cells used for storing a program's input data and its computational results are called variables because the values stored in variables can change during the execution of the program.
The variable declarations also specify the type of data that can be stored in each variable and how the memory for each variable is allocated.
In PHP, a variable declaration has the following syntax:
$variable_name;
Thus, a dollar sign ($) and then the variable name. Every statement ends with a semicolon so the declaration also ends with a semicolon.
For example, the declaration $radius; tells the compiler to reserve a memory cell named radius to hold data and that the compiler should allocate memory for it accordingly.
NOTE: In PHP, the data type is not stated when declaration variables.
In modern php version, you can specify the data type to ensure the memory cell holds the appropriate type of data.
NOTE: These data types are only used in functions and classes. We will later consider functions and classes.
If you are using php version 7 and above, you can choose to specify the data type or not.
Variables are declared to create space on the memory for a data you may have obtained from a user or temporary store on the memory (e.g. a computed value)
After the variable declarations, the program can use these variables to store and manipulate data.
For instance in a program, the variable $radius is used to store the radius of a circle, and the variable $area is used to store the calculated area of the circle.
Variable declarations reserves memory space for each variable based on its data type.
Using a variable without declaring it first is like trying to conceive with no womb
A womb provides the necessary space, environment, and structure for something to exist and grow. If you try to use a variable (the "child") without declaring it (the "womb"), the computer has no memory allocated to hold that data.
The Declaration: You're telling the system, "Hey, I need some space reserved for something specific."
The Error: Without that declaration, the compiler or interpreter hits a wall. It doesn't know what the variable is, how much space it needs, or where it's supposed to live.
It is therefore important to declare variables before using them.
Every programming language has its own rules for giving names to variables.
In PHP the rules are:
When retrieving user's data from the front end (form), variables are declared for each data to store on the memory and validate later.
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$date_of_birth = $_POST['date_of_birth'];
$country = $_POST['country'];
?>
A constant is a value that cannot be changed during the execution of a program.
Unlike regular variables, constants do not use a dollar sign ($) and are globally accessible across your entire script automatically.
There are two ways to define constants in PHP, and they have distinct differences in where they can be used:
Method A: Using the define() function (Runtime)
This creates a constant at runtime. It can be used anywhere in your code, including inside conditional statements (if blocks).
<?php
define("MIN_AGE", 18);
define("SITE_URL", "https://kodevibe.com");
echo MIN_AGE; // Outputs: 18
echo "<p>Site:".SITE_URL."</p>";
?>
Method B: Using the const keyword (Compile-time)
This creates a constant at compile-time. It is cleaner and faster, but it cannot be placed inside conditional blocks or functions. It must be used at the top-level scope or inside a Class.
<?php
const MAX_LOGIN_ATTEMPTS = 3;
echo MAX_LOGIN_ATTEMPTS; // Outputs: 3
?>
To practice variable declarations (creating space on memory to store data), create a folder in the php-programming folder and name it "variable-declarations"
Create space on the memory to store the following information with their appropriate values
<?php
$first_name = "Godwin";
$last_name = "Ashong";
$phone = "+233547539118";
$home_town = "Teshie";
?>
If you run your script, you will not see anything on the screen because the information you typed is stored on the memory.
The echo function and the name of the memory cell is used to display the information on the memory.
You can join a text to the information on the memory using full stop (.)
For instance to show the full name information, the first_name and last_name information on the memory could be joined using the script below:
<?php
$first_name = "Godwin";
$last_name = "Ashong";
echo "<p>Name: ".$first_name." ".$last_name."</p>";
?>
NOTE: the text is placed in a p tag (<p>) to show as paragraph.
Modify your script to show the home town and phone number.
<?php
$first_name = "Godwin";
$last_name = "Ashong";
$phone = "+233547539118";
$home_town = "Teshie";
echo "<p>Name: ".$first_name." ".$last_name."</p>";
echo "<p>Phone: ".$phone."</p>";
echo "<p>Home Town: ".$home_town."</p>";
?>
You can also write an html code in a .php file and only start the php script when you want to display memory content.
<?php
$first_name = "Godwin";
$last_name = "Ashong";
$phone = "+233547539118";
$home_town = "Teshie";
?>
<p>Name: <?php echo $first_name." ".$last_name;?></p>
<p>Phone: <?php echo $phone;?></p>
<p>Home Town: <?php echo $home_town;?></p>