How to declare Variables in PHP

author details
AdiPie
11th Jan 2021
2 mins read
Image
How Variables are found in PHP

Variable is used to store the information and they are also used as "containers".

How to Declare PHP Variables

A variable initiated with the $ symbol like $a.

Example

<?php
  $content = "Hi Everyone!";
  $num = 2;
?>

In the above sample, the variable $content will contain the value Hi, Everyone!

Note: When you authorize the text to a variable, but the value inside the quote.

Note: PHP has no statement for declaring a variable like other programming languages. 


PHP Variables

A variable can have a short characterization (like x, y, etc.) or a more enlightening name (color, hostname, total_count). 

Guide for PHP variables:

  • A variable initiated with the $ symbol like $a.
  • A variable name should begin with a letter or the underscore character.
  • A variable name can't begin with a number.
  • A variable name contains only alpha-numeric characters & underscores like (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($color and $COLOR are two distinct variables)

Output Variables

The echo statement is frequently used to output information to the display.

Example

<?php
  $content = "Well explained";
  echo "The above blog is " . $content . "!";
?>