Contact US


Get Your Project Done by our experts
info@fundaofwebit.com

Variables in php

Creating/Declaring a variable is very simple in PHP.

A variable in php starts with the "$" sign, followed by the name of the variable.

In PHP, you don't have to mention the datatype of the variable when you declare it. You dont have to declare the variable first and then use it. The variable is created when you assign a value to it.

Example:

<?php

$message = "How are you?";
$a = 5;
$b = 10-8;

?>


In the above example, $message will have the value "How are you?" , $a will be assigned with the value "5" and $b will be having the value "2". 

REMEMBER: When assigning a string to any variable, put it inside the double quotes.

Any value/message typed in double quotes(" ") will be display the string as it as.

Example :

<?php

$message = "How are you?";
$a = 10-8;
$b = "10-8";

?>


Here $message will have the value "How are you?", $a will have the value as "2" and $b will have the value "10-8". By this we understand anything written inside the double quotes is a string or a message.

Concatenation of variables

In PHP, We can concatenate two or more variables using the dot "."

Example :

<?php

$message = "How are you?";
$a = 5;

echo $a." is the number and ".$message." is the string";

?>

 

The output for the above code will look something like this:

5 is the number and How are you? is the string

Naming Conventions

1.A variable name can be any aplha-numeric combination but it should always start with a alphabet.

For example

<?php

$a$name$house_no$student5

?>

 

2.We cannot use any special characters/symbol except underscore(_) while naming a variable.

 3.Keywords cannot be used as variables.

Keywords are those reserved words which have predefined meaning.

For example: array, empty, final are few keywords in php.