Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit in PHP to the length of a variable name or function name?

If I wanted to write a really long variable name like:

$this_is_my_variable_that_does_something_in_the_following_function_and_provides_some_information_with_which_the_function_relies_upon_to_do_all_the_work_it_needs = null; 

would that work? same question for function/method names

like image 687
james Avatar asked Dec 30 '10 19:12

james


People also ask

How long can a PHP variable be?

So, is there a limit in size to a Php variable to stores value, in this case, a string ? Yes. A string can be as large as up to 2GB (2147483647 bytes maximum). You can override this limit by increasing memory_limit directive in php.

Does the length of a variable name matter?

It doesn't matter.

What is the maximum size of a variable name?

ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores.

Can variable names be long?

Descriptive variable names should be between 8-20 characters. This is not a restriction, but just a guideline for the length of variable names. If it's too long, then it's really hard to type. If it's too short, it may not be descriptive enough.


2 Answers

Generally, such a limit is imposed by the threat of violence from other folks who interact with your code.

like image 77
ceejayoz Avatar answered Sep 23 '22 08:09

ceejayoz


From the documentation:

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

The same is the case for function names, as stated here.

like image 34
Klaus Byskov Pedersen Avatar answered Sep 22 '22 08:09

Klaus Byskov Pedersen