php variable
php variable
PHP uses the convention of prefixing a dollar sign ($) to the name of a
variable.
Variable names in PHP are case-sensitive.
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.
As per the naming convention, "$name", "$rate_of_int", "$Age", "$mark1"
are examples of valid variable names in PHP.
Invalid variable names: "name" (not having $ prefix), "$rate of int"
(whitespace not allowed), "$Age#1" (invalid character #), "$11" (name not
starting with alphabet).
Variables are assigned with the "=" operator, with the variable on the left
hand side and the expression to be evaluated on the right.
<?php
$x = 10;
echo "Data type of x: " . gettype($x) . "\n";
$x = 10.55;
echo "Data type of x now: " . gettype($x) . "";
?>
It will produce the following output −
Data type of x: integer
Data type of x now: double
PHP does a good job of automatically converting types from one to another
when necessary. In the following code, PHP converts a string variable "y" to
"int" to perform addition with another integer variable and print 30 as the
result.