Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number as variable name in php

Tags:

variables

php

number as variable name not possible right? But this works

 ${4} = 444;
 echo ${4};

Question: How much is this justified using this syntax? and where is info about this in documentation? I not found.

like image 242
Oto Shavadze Avatar asked Oct 03 '12 12:10

Oto Shavadze


People also ask

What are the rules for naming variables in PHP?

Rules for PHP variables: 1 A variable starts with the $ sign, followed by the name of the variable 2 A variable name must start with a letter or the underscore character 3 A variable name cannot start with a number 4 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) More items...

Can a PHP variable name contain any symbol except underscore?

A variable name shouldn’t contain any symbol except underscore. It may be a combination of alphabets and numbers but numbers must not be placed at the starting point. In PHP, a variable name cannot contain spaces.

How to check if a PHP variable is an integer?

PHP has the following predefined constants for integers: PHP has the following functions to check if the type of a variable is integer: Check if the type of a variable is integer: A float is a number with a decimal point or a number in exponential form. 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats.

How to assign a variable to a data type in PHP?

PHP automatically converts the variable to the correct data type, depending on its value. After declaring a variable it can be reused throughout the code. The assignment operator (=) used to assign value to a variable.


2 Answers

The syntax is covered in Variable variables. No, you are not "justified" in using this syntax. You should absolutely never do this, there is no good reason for using a number as a variable name.

like image 117
meagar Avatar answered Sep 29 '22 12:09

meagar


Variables between brackets are considered valid (variable variables), no matter the syntax.

${'sad asda sda'} = 444;
echo ${'sad asda sda'};
// still works.
like image 33
Mihai Iorga Avatar answered Sep 29 '22 12:09

Mihai Iorga