Variable Declarations in PHP

Variables are used for storing the data like strings, numbers , arrays etc in programs.

In PHP a variable starts with a $ sign. Forgetting to add $ sign is a very common mistake for the beginners.

Example for a PHP variable : $variable

And the declaration will look like  $variable = VALUE ;

A PHP variable name can start with a letter or underscore ( _ ) symbol and it shouldn’t contain any space or other special characters like *, % , # etc.

See the valid and invalid variable names

  • $employee name   ( Invalid variable )
  • $employee_name ( Valid variable )

There is no type declaration is needed in PHP variable declaration.variable will automatically converted to the type of assigned value to the variable.

For example :

  • $variable = 10 ; ( Here $variable will treat as integer automatically, we dont need to specify type as Integer at the declaration command )
  • $variable = “I love you”; ( Here $variable will treat as a string automatically, we dont need to specify type as string at the declaration command )

PHP Sample program with variable declaration

<?php
$variable_age = 30;
$variable_name= "Syam";
$variable_desp="has aged";
echo $variable_name."  ". $variable_desp."  ".$variable_age;
?>

The result will be look ke

Syam has aged 30

One thought on “Variable Declarations in PHP”

  1. Automatic type conversion is one of the main advantage and feature of PHP programming.

Comments are closed.

Exit mobile version