Variables in PHP

Notes:

PHP Variables
Variable:
Variable is a named memory location, whose value may change during the execution of a program.

Constant:
Constant is a named memory location, whose value never changes during the execution of a program.
I.e. constants stay constant whereas variables vary.

While building any web application (ex. banking, game etc.) we come across variety of data and data values. According to the requirement of the application we should be able to store and process them. To store and process data and data values we should be able to grab computer memory locations and access them within a script.

To grab a chunk of memory and access it within the script, we need to declare a variable or a constant.
Declaring a variable or a constant: means allocating a memory location for some data

Initializing a variable or a constant: means putting an initial data value within that allocated memory location

In order to change the value in a variable or constant, we need to assign a new value to it.
Assigning a variable or a constant: means putting a new data value within that allocated memory location

Syntax:
// declaration and initialization of a variable
$nameOfVariable=initial value;

// assigning new value to a variable
$nameOfVariable = new value;

Note:
Variables must be initialized, before using them.
Constants must and should be initialized, when they are declared or defined
No comma separated variable or constants allowed, like in other languages
While accessing a variable directly within a string, it’s recommended to enclose a variable within {}

Example Code:

<?php

$playerName = "Manjunath";
echo $playerName,"<br/>";

$playerScore = 0;
echo $playerScore,"<br/>";
$playerScore=20;
echo $playerScore,"<br/>";
echo "<br/>";

echo "Player name= ",$playerName,"<br/>";
echo "Player score= ",$playerScore,"<br/>";

$isGameOver; // you can declare
$isGameOver = true; // but must initialize
echo $isGameOver,"<br/>"; // to use it
echo "<br/>";

$a,$b; // error

echo "Player name= $playerName","<br/>";
echo "Player name= {$playerName}","<br/>";
?>

Interview Questions:

1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a