|
PHP Syntax Tip
|
|
||
|
PHP Syntax Tip
Only a minor simple tip but which is important to take in mind when coding PHP.
In PHP variable names are case-sensitive. The rest is not case-sensitive, function names, reserved words,... PHP Code: $text = "<p>This is a variable</p>";Though the language syntax allow as to do this it is better practice to use yourself some kind of coding standard (if you are making a job from someone it can impose you which coding standards to use). I would suggest as a good practice to code as is everything where case-sensitive (but the language syntax in some cases is not). |
|
||
RE: PHP Syntax Tip
(03-05-2012 03:45 PM)manfer Wrote:Only a minor simple tip but which is important to take in mind when coding PHP. That's a good advice and, in my opinion, that will also help to acquire some discipline when one starts coding in a Language that is 100% case-sensitive, such as JavaScript, for example. |
|
||
|
RE: PHP Syntax Tip
Although, that is a nice tip.. Thanks
But IMO, one must not use same names in order to keep it easy for the developers to go through the code and its documentation...
|
|
||
|
RE: PHP Syntax Tip
Yeah, but this tip is important...Lord knows, I've assigned $FooBar enough times and then wondered why $Foobar has a null value....:-)
-CSense |
|
||
|
RE: PHP Syntax Tip
Programmers need not to worry about code case sensitivity, in their job this problem is tested on a computer wherein the computer gives compile errors to unmatched variable names. And even if you forget function names it will proceed to execution. This problem exist on passwords of users too, they forget the letter casing of their passwords.
|
|
||
RE: PHP Syntax Tip
(04-06-2012 05:32 PM)casacuerva Wrote:Programmers need not to worry about code case sensitivity, in their job this problem is tested on a computer wherein the computer gives compile errors to unmatched variable names. And even if you forget function names it will proceed to execution. This problem exist on passwords of users too, they forget the letter casing of their passwords. Programmers should worry about everything concerning their programming style and best practices in the language at hand.. and shouldn't take that nasty habit of relying on something that isn't always there for them !.. |
|
||
RE: PHP Syntax Tip
(04-06-2012 05:32 PM)casacuerva Wrote:Programmers need not to worry about code case sensitivity, in their job this problem is tested on a computer wherein the computer gives compile errors to unmatched variable names. And even if you forget function names it will proceed to execution. This problem exist on passwords of users too, they forget the letter casing of their passwords. Really the day I read that response on this thread I decided not to answer because is so absurd what is said in that post that really didn't worth the effort. But anyway let's explain why is absurd. 1st How is not going to be important to know if a language is case sensitive or not. If you are coding on a dinamically typed language or typeless language you are not going to get the help of a compiler at all to safe your ass from a misuse of variables. And if you code thinking the language is case insensitive when it is not, you are going to have quite some problems if you mix case thinking you are using same variable. On a statically typed language a compiler is going to be useful because we have to declare variables types before we use them so obviously if we are using My_variable and my_variable thinking is same variable declaring it only once the compiler will get the error. As well if we were using a case insensitive language and try to declare 2 variables with same name and different case the compiler will generate a syntax error notifying about that duplicate variable declaration. But this thread is about PHP which is a dynamically typed language so you are not going to declare variables and it is not going to find any error if you use $My_variable and $my_variable if you use them thinking they are the same. There isn't any syntax error. It is even possible your code has not runtime errors but only an incorrect result. 2ยบ Serious software companies have strict coding style rules of their own. If you are a coder from a company working on a project for them if you don't follow those rules can lead to your project being refused. And I'm sure that if a worker of one of those companies constantly breaks that coding styling rules will be fired very soon. Readability is something very very very important for software maintenance and reading code from a style you are used to is much easier than reading code with other style. (and if it has not style at all is quite difficult). The case of variables, classes, constants, functions, ..., is part of that coding style rules. Other very very important feature to improve code readability is the use of comments as much as possible -obviously not abusing them but including all the necessary ones-. Otherwise after sometime even the coder is not able to understand his own code. Not to mention it is harder to find syntax errors on a bad formatted code than in good formatted one. Even on errors captured by a compiler. And if those are runtime errors even more difficult. ----------------- And about not using same name as a good practice as @Ankur says, obviously for the readability of code that's very important and one though possible in a case sensitive languages should not use variables with same name and different case to avoid confusion. It is better to avoid in same code the use of: myvariablename myVariableName my_variable_name myVariablename ... as different variables. As at the end that code is going to become unreadable. If we are used to a strict naming convention this is not going to happen. For example if we use always lower camel case for variables names we can't have those kind of similar variables. In lower camel case we will only use myVariableName. Anyway this is just common sense. Even with strict conventions if you want you can use confusing variables or functions. For example: function add(aaaaaaaaaa,aaaaaaaaaaa){return (aaaaaaaaaa*aaaaaaaaaaa)-aaaaaaaaaa;} That has nothing wrong on the syntax but using as name of a function add when it multiplies and substracts and using as arguments aaaaaaaaaa and aaaaaaaaaaa that are almost the same, is for sure not good for readability. Anyone reading that code doesn't know without paying great attention what the code is doing. It is not easy at first glance to know if that function is doing: argument1 * argument2 - argument1 argument1 * argument2 - argument1 argument1 * argument1 - argument2 argument1 * argument1 - argument1 ... ... This problem is solved by having as rule not to use meaningless variable names (unless in specific cases, for example it is common practice to use just i as the name of the control variable of a loop,...). |
|
||
|
RE: PHP Syntax Tip
Well said manfer !.. and I hope that our friend there got that message loud and clear .. without having to learn it the hard way !!..
|
|
||
|
RE: PHP Syntax Tip
My advice... not just for PHP but for any language really: comment your code and indent it properly. Sounds simple, but it's also so simple not to do, so when you go back later to work on an old project, it can be really confusing if you don't follow this.
|
|
||
|
RE: PHP Syntax Tip
Well I thought my computer does this case sensitivity checking for me. There is this open source program called "Eclipse" => provides other distribution of PHP environment IDE's, which really checks/helps in coding standards. As long as your computer (3.0ghz processor 4GB memory) can work with these programs , your PHP compiler in the network will work faster detecting coding problems like case sensitivity.
|
|
||
|
RE: PHP Syntax Tip
Hi Manfer, i agree with ankhur.. It seems that what you are implying would be benificial to those who are forgetful on their codes..
|

Forum Home
Terms & Conditions
Server Stats
Search
But IMO, one must not use same names in order to keep it easy for the developers to go through the code and its documentation...