Getting Started With PHP 3


So I’ve mentioned a few times that our shop is switching to PHP, and we’re actually getting started this week.  While my partner works on figuring out how to set up CakePHP, the framework we’re leaning towards, I’ve started doing some PHP tutorials. Nothing like having to learn a “new” language than to make you feel stupid again. 😛  Though in this case, I did know PHP once, long ago, back when it was PHP 3 and then a bit of 4.  But PHP 5 and OO PHP?  New to me.

I considered the Zend PHP tutorial, but the tone of it just kind of put me off, so I am going through the simply named PHP5 Tutorial and figured I’d share some of my early experiences/thoughts as I go along (an idea totally and shamelessly stolen from Adam Cameron 😉 ).

The first few bits go through installation, so I skipped most of that since it was windows focused (and I already know how to do it).  I also skipped the bit on the an editor called TSW WebCoder as I’m not interested in jumping from Dreamweaver (already enough switching going on) and that IDE mimics Windows Ribbon design which I truly abhor 😛

Into the basics of PHP. I remember how to echo and doing tags and variables and all.  So far, most of it is fairly basic.  I did the usual “first bit of code”, sort of.

<?php
        echo "The cake is a lie";
?>

My first try I just did the short tag form, i.e. <g;? but that failed.  So added the php bit and it worked (and got the PHP errors turned on for our development server where I’m practicing all this LOL).  From there, into variable setting and a little iffing.

<?php
        $startAge = 18;

       if ($startage == 18)
               echo "You can go to college, vote, and smoke";

       else
               echo "You aren't 18, go away";
?>

I hate the double equals thing for comparisons, seriously. When I did PHP years ago, I was constantly forgetting to double it up to say equals – it just makes no logical sense to me.  According to the tutorial, you can also now do a triple equals to include type comparison?  So is it better to just always do triple so that you get a more correct comparison or if you’re dealing with “strings” vs “numbers”, double is better to avoid issues between the two? Question to study later.

For now…ternary operators.  Ter…what??  Who made up that word? And WTF?

<?php
        $startAge = 21;
                echo (($startAge > 21) ? "You can also drink" : "you're stuck in middleville");
?>

I no like that at all – completely unreadable, IMHO.  I like clean, easy to read code.  I suspect I’ll skip using that method of iffying.  But first, I’ll go ahead and leave it there and try a while loop.

<?php
    $startAge = 17;
    
    while ($startAge < 22) {
        echo "<blockquote>Current age: " . $startAge;
        
        if ($startAge == 18) {
            echo "<p>You can go to college, vote, and smoke";
            echo (($startAge >= 21) ? "<p>You can also drink and join the military" : "<p>you're stuck in middleville");
        }
        else
            echo "<p>You aren't 18, go away";
        
        $startAge++;
        echo "</blockquote>";
    }
?>

That’s fairly simple and easy to remember. I’ll admit, that while loop code is mildly easier than ColdFusion’s. So then…a do-while loop?  Why did we need another variant of the while loop? So do-while runs at least one always…but why would we use it versus a while loop?  If the while loop doesn’t run, the condition wasn’t true – that seems right to me.  Maybe as I get into some “real” coding, I’ll find a use for it.

For loops I remember and while I dislike the general syntax, it’s easier to pick up.  For each loops sound familiar, except I think the syntax changed a little? Or maybe back in the day we did this a harder way with for loops?  Anyway, so foreach is only for arrays?  Well, pooh…ColdFusion can loop arrays, lists, structs…and the tutorial hasn’t covered arrays, so skipping the code example beyond glancing at it now.

Includes are pretty simple, even with the four methods.  I actually remember those as during my last bit of PHP app development I learned the hard way the importance of require_once versus require 😛  The last bit of the section on basic PHP is on writing functions.  Yay! Something I am sure we will use a lot as we intend to do “proper” PHP as well as try to stick with doing MVC and OO type coding.

<?php



    function giveMeString ($lengthOfString = 5) {
        $myString = "";
    
        for($count = 0; $count < $lengthOfString; $count++) 
            $myString = $myString . chr($count + 100);
    
        return $myString;
    }



    echo "First run: " . giveMeString();
?>
<br /><br />
<?php
    echo "Second run: " . giveMeString(10);
?>

For this function I cheated a little and used the chr function, which hasn’t been covered yet but I remembered it. I like that I can set default values for stuff, we use that quite a bit in our CF functions now.  But unlike ColdFusion, all optional parameters must be at the end?  And there appears to be no way to indicate which value is for which parameter, so you always have to do them in exactly the right order?

Also, PHP now concats strings with a dot?  Didn’t it used to be an ampersand?  Guess we’ll cover concating more in the next chunk which is data types.

Honestly, the tutorial is a bit boring (though well written, so not its fault) and it isn’t my favorite way to learn, but for the basic stuff I need some grounding.  Once I get through these, then I’ll probably just go – okay, here’s some ColdFusion code, make a PHP version. 🙂  Since it’s near the end of the day, though, I’ll pick up the tutorial again tomorrow…