blog about graphic design, web design and marketing

The D. Drew Design Blog
9
jun 09

How to Talk Like PHP: Part 1

In an effort to help people who, like having done so myself, are moving into web for the first time from other media work, I'd like to try to teach a bit of what I've learned. This lesson is also continued on How to Talk Like PHP: Part 2.

It took me a while to figure it out, but I learn code best when I can convert what I see into plain English. Applescript is a great example of this plain English approach to code. Something like this is par for the course:


tell application "Finder"
activate
select window of desktop
select window of desktop
make new Finder window to startup disk



Realizing I handled this interpretation of coding well, I started looking for examples where people had explained PHP in this fashion. Noting that I found very few, and figuring there are a lot of people out there like myself, I wanted to start here. I'm going to break down some code you might commonly be working with in PHP.

Example 1

<?php
$x = 'Hello World';
echo 'Hello World';
echo $x;
$x .= ', says Tom';
echo $x , ', Hello!';
?>



What's happening here is the code writer is embedding this bit of PHP directly into a web page. To signal to a page that you have PHP code in it to be activated, you need to start with the '<?php' tag and end with the '?>' when you are finished. Why the syntax requires a question mark, I'll never know, but just think of your PHP code as pages in a book, and the '<?php' being the front cover, and the '?>' being the back cover.

In the next line, you see a variable, noted as '$x'. Variables can hold just about anything you can think to put into them. In this case, we are putting a "string" of text into the variable by using the equals sign. The string is encompassed in single quotes to let PHP know that it is a string. If you try to run that line without quotes around Hello World, you'll get a syntax error. What that equals sign is saying is "Hey, PHP, I want you to take the information on the left, and put it into that variable on the right." If you had a bucket and sand, the variable would be the bucket, and the value would be the sand. That simple. Also notice at the end of each line is a semicolon. That tells PHP to "Stop doing this line's command here, and continue to the next procedure." In this case, it's simply the next line.

On the line following, you see the "echo" command. As soon as PHP sees that "echo" it's going to want to output any information following that command. In this case, we are telling it to output the string "Hello World". If you ran just that command, you would see:


Hello World



Notice you don't see the quotation marks. They aren't part of the string. They just tell PHP that what is inside those quotations [i]is[/i] a string.

Incidentally, the following line "echo $x;" would give the same exact output. The reason, of course, is that you had already told PHP above that you wanted that variable to contain the string "Hello World". Notice, however, that you do not need to put that variable in quotation marks. If you did, you would receive this output:


$x



Again, the lesson here is that [i]anything[/i] you put in quotations is deemed a string of text and is treated as such.

On the following line '$x .= ", says Tom";', you see something irregular, the ".=" character. This character in and of itself is a command. The way you would read this line is this: "Take whatever value is in the variable $x and [b]add to the end of it[/b] the string ', says Tom'". Basically it appends information to strings in variables, or per my earlier analogy, adding more sand, without changing the sand already in the bucket.

Another way to append information to variables, but in a different way, appears on the next line. You'll notice the variable $x followed by a space, a dot, a space, and then ', Hello!' in single quotes. This illustrates another nuance of PHP: [b]It doesn't care whether you use double or single quotes to illustrate strings, just so long as you don't mix them up[/b]. If you mix them up, things get misinterpreted, but we'll cover that later. For performance reasons, however, you should always encompass your strings in single quotes.

So going back to that last line, what's happening is that, rather than changing the variable's value (the amount of sand in your bucket), you're simply adding information (sand) off on the side. Your output from that last line should look something like this:


Hello World, says Tom, Hello!



Don't forget that we added that information from the 4th line to the variable. Now, you can see that the comma placed between the variable and the string text does is essentially add them together in the final string. You can do this with variables and variables, strings and variables or strings and strings (and there are plausible reasons for any of the three). In other examples, you may see a dot (period) being used. This is okay too, but the comma is a better option for performance.

That's it. Please feel free to contact me if you have any questions at my e-mail. If you're ready, there's a Part 2 to this lesson. Thanks, and take care.

top categories of graphic design, web design and marketing articles, listed from most frequent



graphic design, website design, and marketing articles listed by date, started from most current, and organized by articles per each month