PHP comments: # vs. // - php

Lately I've been using # instead of // for doing single line comments inside my code. However, I see most of the people prefer //.
Is there a particular reason for preferring them instead of #? Performance? File weight? Anything?

It doesn't change a single thing, I'd say ; it's just a matter of habits :
// comes from C, and exists in several languages used by lots of people.
# comes from shell and Perl, and exists in not as many "important" languages as // -- so less people use it.
And why are those two both available in PHP ? Well, PHP has been built with people knowing both C, Shell, and Perl, and they have brought what they liked from those languages ;-)

I'd probably stick with // simply because it's the same in JavaScript, and when you code PHP, you're likely going to have some JavaScript in your files as well. (Most websites use JavaScript nowadays.)
This way, your comments look the same, regardless of the language, and it'd ease readability a small bit.

I would only suggest you use // or /* */. As your programs grow you may wish to use a documentator like phpdoc to automate documentation for your program and most of these documentators as well as IDE's will only accept these two.

For single line comments, there is no significant technical reason for prefering // over the octothorpe (that's what the Zend PHP 5 Certification Study Guide calls #) or vice versa. // seems to the more common choice out there in the wild.

I think it's merely a matter of taste and preference. The # as comment marker has its roots in shell scripts while // goes back to C++ so depending on the people who read your code and their background one or the other might look unfamiliar.

You can use #, but // is much more common.
It is also the agreed upon Code Convention in PEAR.

IDE support may be a reason. For instance, with Eclipse you can automatically comment and uncomment blocks with //, but not with # (with the PHP plugin; the Python plugin does #). Looking at it that way, it depends on the IDE you and your collaborators use.

"//" seems more consistent with the "/* */" block comments to me. But apart from that, I know of no reason to prefer one over the other.

Both of them are serving the same exact purpose: commenting out some piece of code.
I'd prefer # because it takes 1 byte. // takes 2 bytes. ;)

Related

What is the difference between # and //? [duplicate]

Lately I've been using # instead of // for doing single line comments inside my code. However, I see most of the people prefer //.
Is there a particular reason for preferring them instead of #? Performance? File weight? Anything?
It doesn't change a single thing, I'd say ; it's just a matter of habits :
// comes from C, and exists in several languages used by lots of people.
# comes from shell and Perl, and exists in not as many "important" languages as // -- so less people use it.
And why are those two both available in PHP ? Well, PHP has been built with people knowing both C, Shell, and Perl, and they have brought what they liked from those languages ;-)
I'd probably stick with // simply because it's the same in JavaScript, and when you code PHP, you're likely going to have some JavaScript in your files as well. (Most websites use JavaScript nowadays.)
This way, your comments look the same, regardless of the language, and it'd ease readability a small bit.
I would only suggest you use // or /* */. As your programs grow you may wish to use a documentator like phpdoc to automate documentation for your program and most of these documentators as well as IDE's will only accept these two.
For single line comments, there is no significant technical reason for prefering // over the octothorpe (that's what the Zend PHP 5 Certification Study Guide calls #) or vice versa. // seems to the more common choice out there in the wild.
I think it's merely a matter of taste and preference. The # as comment marker has its roots in shell scripts while // goes back to C++ so depending on the people who read your code and their background one or the other might look unfamiliar.
You can use #, but // is much more common.
It is also the agreed upon Code Convention in PEAR.
IDE support may be a reason. For instance, with Eclipse you can automatically comment and uncomment blocks with //, but not with # (with the PHP plugin; the Python plugin does #). Looking at it that way, it depends on the IDE you and your collaborators use.
"//" seems more consistent with the "/* */" block comments to me. But apart from that, I know of no reason to prefer one over the other.
Both of them are serving the same exact purpose: commenting out some piece of code.
I'd prefer # because it takes 1 byte. // takes 2 bytes. ;)

PHP preprocessing script? [duplicate]

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

Is there anything like CoffeeScript for PHP?

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

CoffeeScript-esque language for PHP? [duplicate]

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

Getting to know a new web-system that you have to work on/extend

I am going to start working on a website that has already been built by someone else.
The main script was bought and then adjusted by the lead programmer. The lead has left and I am the only programmer.
Never met the lead and there are no papers, documentation or comments in the code to help me out, also there are many functions with single letter names. There are also parts of the code that are all compressed in one line (like where there should be 200 lines there is one).
There are a few hundred files.
My questions are:
Does anyone have any advice on how to understand this system?
Has anyone had any similar experiences?
Does anyone have a quick way of decompressing the lines?
Please help me out here. This is my first big break and I really want this to work out well.
Thanks
EDIT:
On regards to the question:
- Does anyone have a quick way of decompressing the lines?
I just used notepad++ (extended replace) and netbeans (the format option) to change a file from 1696 lines to 5584!!
This is going to be a loooonnngggg project
For reformatting the source, try this online pretty-printer: http://www.prettyprinter.de/
For understanding the HTML and CSS, use Firebug.
For understanding the PHP code, step through it in a debugger. (I can't personally recommend a PHP debugger, but I've heard good things about Komodo.)
Start by checking the whole thing into source control, if you haven't already, and then as you work out what the various functions and variables do, rename them to something sensible and check in your changes.
If you can cobble together some rough regression tests (eg. with Selenium) before you start then you can be reasonably sure you aren't breaking anything as you go.
Ouch! I feel your pain!
A few things to get started:
If you're not using source control, don't do anything else until you get that set up. As you hack away at the files, you need to be able to revert to previous, presumably-working versions. Which source-control system you use isn't as important as using one. Subversion is easy and widely used.
Get an editor with a good PHP syntax highlighter and code folder. Which one is largely down to platform and personal taste; I like JEdit and Notepad++. These will help you navigate the code within a page. JEdit's folder is the best around. Notepad++ has a cool feature that when you highlight a word it highlights the other occurrences in the same file, so you can easily see e.g. where a tag begins, or where a variable is used.
Unwind those long lines by search-and-replace ';' with ';\n' -- at least you'll get every statement on a line of its own. The pretty-printer mentioned above will do the same plus indent. But I find that going in and indenting the code manually is a nice way to start to get familiar with it.
Analyze the website's major use cases and trace each one. If you're a front-end guy, this might be easier if you start from the front-end and work your way back to the DB; if you're a back-end guy, start with the DB and see what talks to it, and then how that's used to render pages -- either way works. Use FireBug in Firefox to inspect e.g. forms to see what names the fields take and what page they post to. Look at the PHP page to see what happens next. Use some echo() statements to print out the values of variables at various places. Finally, crack open the DB and get familiar with its schema.
Lather, rinse, repeat.
Good luck!
Could you get a copy of the original script version which was bought? It might be that that is documented. You could then use a comparison tool like Beyond Compare in order to extract any modifications that have been made.
If the functions names are only one letter it could be that the code is encoded with some kind of tool (I think Zend had a tool like that - Zend Encoder?) so that people cannot copy it. You should try to find an unencoded version, if there is one because that would save a lot of time.

Categories