×
☰ See All Chapters

PHP Variables

  • A variable is used to store information like text, numbers, arrays, date etc…  

  • When a variable is declared, it can be retrieved and manipulated over and over again in your script. 

  • The assignment operator (=) used to assign value to a variable. 

<!DOCTYPE HTML>

<html>

<head>

<title>PHP Example</title>

</head>

<body>

<?php

$full_name="Manu Majunatha";

echo $full_name;

?>

</body>

</html>

Variable Naming Rules

All variables in PHP should start with a $ sign symbol. Below is an example for PHP variable declaration.

$var_name = value;

Variable Naming Conventions

  • Variable names should not contain spaces as $full name, correct way of declaring it is $full_name. 

  • Name can comprise letters, numbers, and underscore ( _ )  characters 

  • The first character after the $ dollar sign must be a letter or an underscore character ( _ )  it cannot be a number. 

  • If a variable name is more than one word, it should be separated with an underscore ($full_name), or with capitalization ($fullName) 

PHP is a Loosely Typed Language

  • In a strongly typed programming language like Java, you have to declare (define) the type and name of the variable before using it. 

  • PHP is loosely typed language that you do not have to tell PHP which data type the variable is. 

  • PHP automatically converts the variable to the correct data type, depending on its first assigned value. 

  • From PHP 7 onwards you can specify type to a variable and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch. You will learn more about strict and non-strict requirements 


All Chapters
Author