PHP BASICS

Lesson 2.Variables


In PHP, Variables are like buckets

Variables are used to store information like numbers, text, images, logic

The stored information are for later use

The basic syntax for a variable is $variable_name="my first variable" where my first variable is the content of the variable "variable_name"

There are 4 basic types of variables and each holds specific information

1. Boolean Variable: stores values of true or false
2. Integer Variable: stores whole numbers
3. Floating Point Variable: usually a fractional number, with a decimal.
4. String Variable: Simple text that must be enclosed within double quotations " " or single quotations ' '
=============================================
BOOLEAN VARIABLE (e.g. $logged_in = true;) between the php tags
=============================================
INTEGER VARIABLE (e.g. $fav_num = 200;) between the php tags
=============================================
FLOATING POINT VARIABLE (e.g. $top_speed = 104.87;) between the php tags
=============================================
STRING VARIABLE (e.g. $vehicle = "Santafe";)
=============================================

Refer to Procedure below

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 between the PHP tag

< ?php

    $myname = "Charles";

    $myage = "40";

    $favcolour = "blue";

To display these variables on a page, we use the PHP function "print"

    print("My name is $myname! < br> I am $myage years old, and like the colour $favcolour.");

?>

Output: My name is Charles!
I am 40 years old, and like the colour blue.