I have been working for years with client-side scripting wonder Javascript. Over that time I had dabbled in some ASP, and in some PHP even less. Server-side scripting was more of an “as-needed” item in my undertakings, but I knew in time I wanted to learn more. A short while ago I started working through a thorough book on ASP.NET. About a third in, I took a step back and started down my journey of really discovering what I should have in my coding toolkit as a Web Developer. PHP emerged as an important tool to have and I decided to journey there first instead. ASP.NET will be more intertwined into my PHP learning instead of at the forefront for the moment, but I realize their similarities and am excited to develop skills in both over time. Oh I have the plans my friend, I have the plans…
Hence I found this great little book on getting started with PHP called “PHP The Good Parts” (MacIntyre, Peter B. PHP: The Good Parts. USA: O’Reilly, 2010). This is the first in a series of articles I will post as I work through the chapters of this book.
Chapter Two — Casing the Joint — is where are the fun really begins. This chapters explains the basic “building blocks” – creating variables and constants, using controls statements and storing session data. I was happy (relieved!) to see how similar many of these elements in this server-side scripting language are to my familiar client-side scripting language, Javascript.
In my earnest first reading (i.e. I will probably correct myself later on some of these points!), some distinct differences I noticed in the basics:
- If you want to write something to your HTML page in Javascript, you use
document.write
while in PHP, you use
echo
- PHP requires “$” at the beginning of variable names.
- If you want to add together strings of text in Javascript, your basic option is
myString1 + myString2
whereas in PHP, you use
$myString1 . $myString2"
That one may take a little getting used to!
- The scope of PHP variables (outside of its global constants) is a bit more particular than Javascript. In Javascript, you can just create a variable at the top of your .js file and any function within the file or within the HTML page can see and modify it. By default with PHP, a variable created at the top of your .php file will be available within the page, but not within any functions on that page. This can be changed by setting scope parameters, which the book promises me it will do a little later on.
- PHP files are named .php, and run on their own, i.e. in place of your .htm/.html file, since they will be serving up your HTML code. You start your PHP page with
<?php>
and end it with
<?>
Javascript on the other hand is something you include into your .htm/.html file, either with tags or by including the .js file in your HTML file, and then within that .js file just listing out your immediately running code and functions i.e. similar to the code that goes in
<?php><?>
Outside of the basics, the explanation of the various session data storage methods – GET, POST, COOKIES and REQUEST was very helpful, and this is one of the first items that reminded me I’m not in Client-Side anymore, Dorothy. I have always used aspects of these before with Javascript — a form here, some peeling of name/value pairs off of a url there — but I look forward to really using (the best) of these features to their potential in my PHP scripts to dynamically write my HTML pages.
Then onward and upward to the other chapters including objects and database programming. Stay tuned as I put this knowledge into action!