PHP Variable


PHP Variable:– Like other programming languages Java,Perl,.Net all variables are used to store the data so same in php also variables are used to save data for processing in program for generating outputs.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Only the differences comes when we use them in different languages. So we will here how to declare and initialise a variable in php language that is very easy .

Rules for naming convensions for a variable name:

1.A variable should start with $ symbol followed by any letter from a-z or underscore only.
2.No numeric values are allowed after $ symbol.
3.A variable name never contains space.
4.The variable name is case-sensitive.


PHP Variable | Description

A PHP variable name is started with $ symbol followed by a name.
For example $x=10; is a declaration and initialisation for integer type variable.Similarily we can define a variable name for different data types like string ,boolean ,float etc.
Rest of declaration you can do easily as here we assume that you know programming a little bit earlier.
Syntax for Variable declaration: $varname=variable type;

Here is a simple example how to define a variable in php. Beginners can see below screeshot for invalid variable name generated by localhost on output console screen.

Example

<?php
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var";      // outputs "Bob, Joe"

$4site = 'not yet';     // invalid; starts with a number
$_4site = 'not yet';    // valid; starts with an underscore
$täyte = 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.
?>

Advertisements

Add Comment

📖 Read More