Introducing Variables
×
Home Courses Community Support
Introducing Variables

Variables are used for storing values, like text strings, numbers or arrays, if we need those in further code later. When a variable is declared, it can be used many time in your script.

Below are some properties of variables:

  • Variables are "containers" for storing information.
  • PHP variable starts with the $ sign, followed by the name of the variable.
  • Variable name must start with a letter or the underscore character.
  • A variable name can never start with a number.
  • Allowed characters to use in variables are alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive ($msg and $MSG are two different variables)
  • There are some words which we are not allow using as a name for a variable like $_SESSION, but as we probably do not know which ones are they, our best decision will be to use very descriptive variable names. For example-
    • $my_favorite_song
    • $my_counter
    • $username etc


In the example bellow we have write a PHP page using variable:
 

<?php
$msq = "Welcome to Geeksdemy"; //Variable declaration
echo $msq; //Print Variable value
?>


Check code output on CodeLAB. (Just copy and paste the code in editor.)


In this example, we have defined one variables $msq, pass a value "Welcome to Geeksdemy" and print that variable.
 

enlightenedNew PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work.

 

 

← Preview

PHP in HTML

Next →

Using Constant Values

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App