Creating or defining variables in Php is quite simple and straightforward like other programming languages. If you have knowledge of any programming language, than you just need to learn
$ sign, and all other things are same. In this php tutorial, you will learn how to create Variables in Php.
Variables in Php
Variables are used store data, like string of text, number, etc. Variable value is changeable, they are not fixed. Value of a variable can be change at any time over the course of script. Here are few things, you need to know about creating variables in Php:
- In Php, you need not be to declare as variable, before adding value to it, while creating a variable. By default, Php converts the variable to the correct data type, depending on its value.
- Variables in Php can be reused throughout the code.
- You can change a variable’s value at anytime.
- Variables in start with
- The assignment operator
=used to assign a value in variable.
In Php, a variable can be declared as:
$variable_name = value;
How to create variables in Php
&lt;?php // creating variable $text = "Good Game.!!"; $number = 69; // displaying variable echo $text // Output on screen: Good Game.!! echo $number //Output on screen: 69 ?&gt;
In above example, I have created two Php variables. Where first one has assigned value of strings; Good Game.!! while other has the value in number; 69.
In order to display my variables on the screen, I print them using the
echo statement. The Php
echo statement often used to output and display data in Php. You have already learned about
echo statement in previous lessons.
Rules for naming variables in Php
There are several rules in Php, you need to consider while creating a variable:
- All variables start with a
$sign followed by the variable name.
- A variable name cannot start with number or any sign except
- A variable name must start with a character or
- A variable name in PHP can only contain alpha-numeric characters and underscores (
- A variable name cannot contain space anywhere.
- A variable name in Php is case-sensitive. For example: variable names
$golfare two different variables.