DEV Community

Cover image for PHP Variables Learn| Types Variables| Declare Variables
Learn Coding
Learn Coding

Posted on

PHP Variables Learn| Types Variables| Declare Variables

How to Create Php Variables| Php For Beginners with Examples
Php Variables: Variables is a name of storage space which is used to store data. Its value may be changed. It always contains the last value stored to it. Variables is a container which holds the value. Variable in PHP starts with dollar($) sign. In PHP, a variable is declared using a $ sign followed by the variable name. Here, some important points to know about variables.

As PHP is a loosely typed language, so we do not need to declare the data types of the variables.
It automatically analyzes the values and makes conversions to its correct datatype.
After declaring a variable, it can be reused throughout the code.
Assignment Operator (=) is used to assign the value to a variable.
What is declared syntax?

$variablename=value;

What are variables in PHP, explain with example?

<?php

$x=100;

$x=50;

$x=250;

echo "The value of x is:".$x;

?>

*******OUTPUT*******

The value of x is:250

What is string in PHP with example?

<?php

$str="Learn Coding";

echo "I like $str";

?>

*******OUTPUT*******

I like Learn Coding

Can you concatenate strings in PHP?

<?php

$str="Learn Coding";

echo "I like ".$str;

?>

*******OUTPUT*******

I like Learn Coding

PHP Variables Learn| Types Variables| Declare Variables

Table of Contents
What are the rules to declare variable in PHP?

  1. It must start with the dollar sign.

  2. There is no need of data type (int, float, char) to define a variable.

  3. Variables can, but do not need, to be declared before assignment.

  4. After the dollar sign, the first letter of a variable should be alphabet or underscore(_).

  5. After the dollar sign, the first letter of a variable should not be a digit.

  6. After the first character, it may be a combination of alphabets and digits.

  7. Blank spaces are not allowed in variable name.

  8. A variable must start with a dollar ($) sign, followed by the variable name.

  9. It can only contain alphanumeric character and underscore (A-z, 0-9, _).

  10. A variable name must start with a letter or underscore (_) character.

  11. A PHP variable name cannot contain spaces.

  12. One thing to be kept in mind that the variable name cannot start with a number or special symbols.

  13. PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.
    Read More

Top comments (0)