Writing about PHP Programming

  • Joe Winett Ummm, I will admit that not remembering the details usually doesn’t stop me from telling a story… no cop, no foul.. Or perhaps, not under oath, no foul.
  • Joe Winett I pride myself on always trying to be completely honest these days, but I was driving home earlier, telling a story to the invisible audience in my head… practicing a story… and then I stopped and thought… I pride myself on always trying to be completely honest, and here I’ve just embellished part of that story.. which was implied to be a true one.. it was sorta.
  • Joe Winett How do you write a good story? I wouldn’t know, but I think mine get better as I tell them for a dozen years in my head. To my invisible audience.
  • Joe Winett I have always said that I want to write some sort of book — just because I’ve always wanted to write a book — but I’m usually coming up with excuses not to formally start assembling one. Longtime excuse (love me long time) => “I haven’t figured out an ending for my book.” Here’s probably the real reason => “Sounds difficult.”
  • Joe Winett $unrelatedTrvia += 5;
  • Joe Winett The value in the variable referenced by the name “unrelatedTrivia” is incremented by 5. It’s the same as $unrelatedTrivia = $unrelatedTrivia + 5;
  • Joe Winett In PHP $unrelatedTrivia refers to the same variable as $UNRELATEDtrivia… because identifiers are not case sensitive. THIS IS A DEVIATION from how the “c” programming language works — where identifiers are case sensitive. That’s always bugged me because I’m a c guy… but it makes sense — it leads to fewer bugs because of typos… and this was important because except when operating in a strict mode, php never warned you about undeclared variables… and THAT CAUSES BUGS.
  • Joe Winett The reason? $joe = 5; echo $job; This doesn’t echo 5, it echos whatever is in $job. If $job was never initialized with a value, then it echos nothing… YOU HAVE A PROBLEM!!
  • Joe Winett I always put PHP in explicit mode where the example above would have caused an error to be displayed on the screen saying that I used an uninitialized variable.
  • Joe Winett $unrelatedTrvia += 4; // more
  • Joe Winett Anything after // is a comment.
  • Joe Winett /* Yeppers */
  • Joe Winett Anything between /* and */ is a comment. The difference is that // is a single line comment — the next line of the program is not a comment unless also prefixed by a start of comment token. With /* the comment continues, line by line, until */ is reached. Anything after */ even on the same line is not a comment.
  • Joe Winett In PHP (and c) a statement ends with a semicolon; And you can have another statement on the same line; as often as you like;
  • Joe Winett $joe =1; $betty = 2; $jane = 3; $company = false;
  • Joe Winett However if ( $guests > 2 && $guest1 == “joe” && otherGuestsSex() == “f” ) { $party = true; $company = true; }
  • Joe Winett Anything between { and } is a statement BLOCK. With an if() statement, if the expression is TRUE, then the next statement is executed… If it is not true, then the next statement is skipped. If you want to execute more than one statement on TRUE, then you surround then in curly braces {}. If the evaluation is FALSE, then the execution continues after skipping everything in the block. Putting something in braces creates another SCOPE… In c, the SCOPE is isolated… You can declare new variables with the same name as variables in an “OUTER SCOPE” and they are really referring to NEW VARIABLES… and not the old ones. BUT THIS CAUSES BUGS… for instance if you meant to declare a new variable, but you’ve typed “int x = 1;” so many times that you forgot you need the old x, and now you’ve got a new x, but they’re the same name, so you’ve caused a problem. In PHP, the scope is just about execution and not about variables. Variables can only have a couple of levels of scope (this is simplifying it)… you have GLOBAL SCOPE — meaning the whole program… and then a local function scope… Actually, with classes you sort of have a scope limited to the object…
  • Joe Winett Although PHP’s grammar is much like C’s (which is supposed to be lower case), it is not c. PHP doesn’t have typed variables… $a can hold a string “5”, but if you use the variable in a mathematical expression, then it’s a number. If you use it as a string, then it’s a string…
  • Joe Winett So if you write $x = “5”; $x = $x + 1; then $x is 6… and then you can still use it in a string expression like $vig = $x . “Joe”; and $v is a string that reads “6Joe”. To stick together two strings, you use the dot operator (the period)… This is to avoid the problem of determining whether you’re using the variable as a string or as a number.
  • Joe Winett Consider $x = “4”; $y = 2; $z = $x + $y; $a = $x . $y;
  • Joe Winett Then $z contains 6, but $a contains “42”;
  • Joe Winett $unrelatedTrvia = “Never ends.”;
  • Joe Winett For reasons I’m too tired to write (I’m assuming you care because you’re still reading)… I’m not going to explain why = is not == YET
  • Joe Winett In PHP = is not ==, but === can be just like ==.
  • Joe Winett Also == is not !=

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.