PHP is my favourite programming language of all time. It’s simple when you need it to be, and advanced when you need that extra power. I’m what many people would call an evangelist of PHP. Since starting work at Netbasic, I’ve really gone all out in using object-orientated programming (OOP) with PHP, and nearly everything I do is object-orientated now.
Let’s step back a moment to the title of this article, “PHP Basics”. Isn’t that an indication that this is a guide for beginners? You’d be right in thinking that, and I’m going to go from nearly the beginning. For the purposes of the tutorial, I will assume you know how to set up your own LAMP or WAMP server (and if you don’t there are countless great tutorials on that), and that you know how the internet works and some basic HTML/CSS. I will also assume that you have little-to-no experience of object orientated programming (although understanding OOP in C++ or Java for example would be a bonus!), little-to-no experience of general programming, and no experience of PHP.
The next question you’re probably asking is why am I writing a “PHP introduction” tutorial when there are countless already out there? The answer to that is that I’m trying a different approach that I’ve not seen before. I’m going to start at the object orientated level so that procedural PHP scripting doesn’t even factor into the equation. I’ve done quite a long preamble now with not much in the way of teaching PHP, so we’re going to start now.
Read the rest of this entry »
It annoys me when I see tutorials for learning PHP with code like this:
$fd = @file("doesnotexist");
and this:
$md = mysql_connect("localhost", "user", "pass") or die("Could not connect!");
It’s bad practice in the real programming world and shouldn’t happen. The at symbol (@) is used to silence any errors caused by calling a function as shown above, when proper error handling should be implemented. Similarly, the practice of using “or die” to error check needs to stop being taught.
Using this language in PHP really needs to stop because there are much better ways to handle errors, such as using exceptions, or any sort of error handling except this really. My personal favourite is using an exception and catching it further down the stack. There’s loads of tutorials on error handling so I won’t go into it, but I just wanted to vent my hatred for this poor excuse for shoddy coding.
And while I’m at it, stop using mysql_* functions and start using PDO!
I have a case at work where I needed to go through a big list of arrays, and add them to a new master array. It turns out this is incredibly slow when adding many large arrays to a larger array. The alternative to this if you don’t care about keys or duplication of values is to just add it into the array. So normally where one might do something like:
$big_array = array();
foreach($array_of_objs as $obj)
{
$row = $obj->getSomeData(); // returns an array of data
$big_array = array_merge($big_array, $row);
}
processData($big_array);
I believe that array_merge creates a new array every time which is what was slowing it down. The solution was simple enough, it just took me a while to think of!
$big_array = array();
foreach($array_of_objs as $obj)
{
$row = $obj->getSomeData(); // returns an array of data
foreach($row as $r)
{
$big_array[] = $r;
}
}
processData($big_array);
This was vastly faster, again possibly because it doesn’t create a new array every time. There are limitations to this method, and it doesn’t do exactly the same thing as array_merge, but it does do what I needed it do, so if anyone has a similar situation, I hope this helps!
Srsly PHP, why can’t I do this:
$value = isset($myGPCWrapper->Get("value")) ? $myGPCWrapper->Get("value") : $default;
Resulting in this:
Fatal error: Can’t use method return value in write context in /path/to/my/script.php on line 21
And instead I have to do this:
$foo = $myGPCWrapper->Get("value");
$value = isset($foo) ? $foo : $default;
or
$value = isset($_GET['value']) ? $myGPCWrapper->Get("value") : $default;
I mean… how is isset writing to my variable?!
Writing a bit of PHP tonight and found that if you use parse_ini_file to load an INI file in PHP, there is a slightly hacky way of having double quotes in strings. For example, I tried these:
somevalue = "This ""string"" has quotes"
somevalue = "This \"string\" has quotes"
But none of them worked! Turns out a quick look at the PHP manual reveals a simple (but a bit hacky) solution to this is to define a constant e.g. QUOTE to be “, and use that in the INI string.
// In the PHP:
define('QUOTE', '"');
; and in the INI file
somevalue = "This "QUOTE"string"QUOTE" has quotes"