Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I understand that all PHP variables need to be preceded by a dollar sign (e.g. $someValue = 1), but why is this the case? Why was this design choice made?

The reason I ask is my girlfriend asked why when I mentioned that PHP variables need to begin with a dollar sign, and I realized I had no idea why this syntactic choice was made.

Any ideas?

share|improve this question
8  
Because PHP's creator didn't know how to write a parser. So a crutch was needed to identify identifiers. –  ThomasX Dec 14 '11 at 7:23
    
@ThomasX - Haha you are probably right... I'm no fan of the C code base either (yikes) –  Ben DeMott Dec 14 '11 at 7:51
    
I think that only who created PHP can answer to this question. –  kiamlaluno Dec 14 '11 at 9:35
1  
I gues it is to encourage developer... everytime you put $ sign you're closer to your payment! –  Kamil Klimek Dec 14 '11 at 10:41
add comment

2 Answers

up vote 5 down vote accepted

Well I'm really not a big fan of the $$$ either (I don't really care how much the variable costed). But it comes from perl.

Interpreted scripting languages like perl or php use the $ to quickly identify and parse out variables. It's just an optimization for the interpreter. But definitely not an optimization for the programmer!

share|improve this answer
    
How is it not an optimization for a programmer? - I can immediately identify variables and distinguish them from other constructs .. –  johannes Dec 14 '11 at 9:22
    
I believe Larry Wall once said the $ in front of variables made it easier to extend Perl without breaking existing code. He could add new keywords that would not already be used as variables. It should be this article, but it's behind a paywall :-( –  S.L. Barth Dec 14 '11 at 13:01
    
@johannes I can also immediately identify a variable in C even without the $ because I've memorized all the key words and functions have parenthesis after them. –  Bizorke Dec 14 '11 at 14:46
1  
@Bizorke C probably has less keywords (didn't count them) but well, PHP has constants, without $, even though people usually use constants in UPERCASE. And well, once you know all keywords etc by heart you are advanced, getting there might be simpler with this visual assistance ... but then: This is a matter of taste. My assumption for an answer would be "copied from Perl" and "simpler to parse" aka. historic decision –  johannes Dec 14 '11 at 16:11
    
@johannes I didn't say the $ was copied from perl, I said it comes from Perl; PHP is based on Perl. Yes it is preference. I dislike typing the $ every time and in many editors $ is not selected when you double click on the word (slows down copy/paste). Yes PHP has slightly more keywords than C, but my argument still applies to C++, java, etc. Key words are generally easy to memorize and are usually relatively consistent between languages. Also, PHP is not JUST for beginners. I program in PHP (and many other languages) every day, and I can tell you that the $ has done nothing but slow me down. –  Bizorke Dec 15 '11 at 4:34
show 1 more comment

PHP Was originally a meta-syntax that was parsed and simply dynamically called-into a compiled c-programs functions.
In this way you can think of it similar to a "shell" program for the "web"...
It's no wonder variables with $dollarsigns were used similar to shell variables.

When I was all of 12, and I was learning PHP 3... as my first programming language - having the $ made life so much easier. I knew what a variable was, but having a reminder in all of the source code I read made learning possible... not sure I'd be here today if I didn't start on PHP.

Here's an example from -> http://www.php.net/manual/en/history.php.php

<!--include /text/header.html-->

<!--getenv HTTP_USER_AGENT-->
<!--ifsubstr $exec_result Mozilla-->
  Hey, you are using Netscape!<p>
<!--endif-->

<!--sql database select * from table where user='$username'-->
<!--ifless $numentries 1-->
  Sorry, that record does not exist<p>
<!--endif exit-->
  Welcome <!--$user-->!<p>
  You have <!--$index:0--> credits left in your account.<p>

<!--include /text/footer.html-->
share|improve this answer
    
While I don't doubt that the dollar sign was helpful to you, I wonder if maybe it was the overall simplicity of PHP that had more to do with it. I started with basic Javascript as a teenager and found variables pretty easy to learn, probably because the Javascript examples in those days were so simple. –  Matt Browne Feb 26 '13 at 1:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.