0% found this document useful (0 votes)
3 views

php variable

In PHP, variables are named memory locations prefixed with a dollar sign ($) and are case-sensitive. They can be assigned values without specifying their type, as PHP is dynamically typed and automatically converts types as needed. Valid variable names must start with a letter or underscore and can include letters, numbers, or underscores, while invalid names include those with spaces or special characters.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

php variable

In PHP, variables are named memory locations prefixed with a dollar sign ($) and are case-sensitive. They can be assigned values without specifying their type, as PHP is dynamically typed and automatically converts types as needed. Valid variable names must start with a letter or underscore and can include letters, numbers, or underscores, while invalid names include those with spaces or special characters.

Uploaded by

mdhasan.ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

A variable in PHP is a named memory location that holds data belonging to

one of the data types.

 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.

1.1 No Need to Specify the Type of a Variable

PHP is a dynamically typed language. There is no need to specify the type of


a variable. On the contrary, the type of a variable is decided by the value
assigned to it. The value of a variable is the value of its most recent
assignment.

Take a look at this following example −


Open Compiler

<?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

1.2 Automatic Type Conversion of Variables

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.

You might also like