A variable is something for storing information.
Variables in PHP
Variables are used for storing a values, such as arrays, numbers or text strings.
Once a variable is declared, it could be used in your script, over and over again.
In PHP, all variables start with a $ (dollar) sign symbol.
Below tells how to correctly declare a variable in PHP script:
<?php $var_name = value; ?>
If you forget the $ sign at the very beginning of your variable, your variable will not work.
Below, we declare a variable contains a number and a variable contains a string:
<?php
$txt = “Hello World”;
$number = 16;
?>
PHP is a loosely Typed Language
In PHP, users do not need to declared a variable before adding any value to it.As what showed above, users do not need to tell the PHP what kind of data type the variable is.PHP is able to converts the variable for the correct data type, all depending on the value of variable.In some other strongly typed programming language, users need to declare the data type and name of a variable before using it.
In PHP, variables are automatically declared.
Rules for Naming Variables
A variable in PHP must be started with an underscore”_” or a letter
A variable name only contains underscores and alpha-numeric characters (0-9,a-z, A-Z, and _ )
A variable can not contain spaces. In case, a variable contains more than one word, users can separate it with capitablization ($myString) or with underscores ($my_string)
Leave a comment
You must be logged in to post a comment.