My article on highlighting source code with PHP was published at Sitepoint.com today. It’s a sequel to the previous article about using BB code. Happy reading! URLs: Highlight Source Code in Your PHP Application [published in the PHP & MySQL Reviews and Apps section] http://www.sitepoint.com/article/highlight-source-code-php Use BB Code in Your PHP Application [it’s filed under […]
strcmp() compares two strings and gives -1, 0 or 1 depending on wheather the first string is “smaller”, equal to or greater than the second one. < ?php echo strcmp('aa','bb'); // prints -1 echo strcmp('bb','bb'); // prints 0 echo strcmp('zz','yy'); // prints 1 ?> Thus being said there’s an error in the php certification guide […]
Serializing is a nice way to get a string out of more complicated data structure. It’s probably mostly used to sting-alize array data, although you can serialize scalar data as well. The result of the serialization is no nature wonder, it’s just a string that describes what is serialized and a value of the serialized […]
You can get a specific character from a string as if the string was an array. < ?php $s = 'PHP'; echo $s[0]; // will print "P" ?> This syntax may be confusing, because someone looking at the code might expect that $s is an array. Well it is an array, an array of characters, […]
When sorting an array, for example by using sort() there are three constants you can use to determine how the sorting will work: SORT_REGULAR (default) when you have mixed types of variables in the array it won’t cast them. The results can be … hmm .. interesting. It will sort first all string types and […]
The certification guide demonstrates what is probably the fastest (performance-wise) way to go through the elements of an array – using the array_walk() function. The example in the guide is using a callback function to use the elements of the array. But there’s more than that. Using the array_walk() function you can also make use […]
Yep, array keys are case sensitive. So you can do: < ?php $a = array('key'=> 1, ‘Key’ => 2); print_r($a); ?>and it will print Array ( [key] => 1 [Key] => 2 ) Similarly if you call array_keys() on $a, like: < ?php $a = array('key'=> 1, ‘Key’ => 2); print_r(array_keys($a)); ?>…you’ll get: Array ( […]
Prior to php 4.1. there were no $_POST, $_GET, etc. superglobal arrays, but there were a bunch of other superglobals, like $HTTP_POST_VARS, $HTTP_GET_VARS and so on. The guide mentions this in the forms chapter but there is a typo (repeated several times 🙂 ) so I thought I should mention it. The guide talks about […]
I was wondering – can you redefine the special variable $this when in a method of a class. It turns out that yes, you can. Consider this class: < ?php class Some { function Some(){} function another(){ $this = 5; var_dump($this); } } ?> If you create an object and call the another() method, it […]
How fast/slow are the static methods? I did a test once to try and figure that out. It turns out: Calling a method statically is just a bit faster than calling it “normally” Calling a static method is twice as fast if your object didn’t exist to begin with Static methods are twice as slow […]
The book tells about static methods but it forgets to mention that you cannot use $this in static methods. A static method is pretty much like a normal stand-alone function is not aware about the object as a whole. So you can call the static method when no object of that class exists. In this […]
Just a thought on the exam prep question 1. The question in its essence is “How much is 3 – 5 % 3”; The answer is 1 because 5 % 3 is 2 and 3 – 2 is 1. That’s exactly what the book says but in the explanation of the correct answer there’s a […]
So far (end of chapter 1, just before jumping into the OO part) the book doesn’t say a word about superglobals. I think this is an important topic and I suppose there must be questions about the superglobals on the exam. The best place to get an idea about the superglobals is this PHP.net manual […]
The certification book goes on with the calc_weeks example: < ?php function calc_weeks() { $years += 10; return $years * 52; } $years = 28; echo calc_weeks(); ?> Exactly as the guide says, the $years var in the function won’t get the 28 value, it will get a null instead. But then the guide says […]
Consider the following example from the book that is supposed to illustrate how a parameter is passed to a function by reference, not by value. < ?php function calc_weeks(&$years) { $my_years += 10; return $my_years * 52; } $my_years = 28; echo calc_weeks($my_years); ?> Something wrong? Yes, the function calc_weeks() is taking $years parameter, passed […]
The usual syntax of a for-loop is < ?php for($i = 0; $i < 10; $i++){ // do your thing } ?> where $i = 0 is evaluated once before the loop starts $i < 10 is the condition of the loop, it's executed the number of times the loop executes plus 1. Why plus […]
The cert guide is maybe not 100% clear on the question of breaking out of a loop, but you can break and continue in all types of loops – for, while and foreach (the latter wasn’t mentioned in the loops section of the book). About the continue when in nested loops. Well, it is true […]
Some time ago I’ve created a phpBB mod/hack to use on a bulletin board I manage. Recently I decided to make it available for anyone interested to install it on their boards. I’ve posted it some time ago on phpbb.com but it takes a while for the phpBB team to review all the mods/hacks they […]
What happens when a false value is casted to a string? I.e. what is the result of the following: < ?php var_dump((string)false); ?> The answer is: string(0) “” This is good to know and expect when using some string functions that sometimes return a string and sometimes a boolean. Such functions are strstr(), strpos(), etc.
The source code highlighting was probably the first feature I was missing in WordPress. I briefly googled my options and decided it would be quicker to add the missing feature myself, since it looked pretty straightforward.
There’s a small error in the PHP certification guide in the alternative if-then-else syntax code. This alternative syntax goes like: < ?php if (true) : echo 'it is true'; else : echo 'so not true'; endif; ?> In the example given in the cert guide, the ; after endif is missing, which will cause a […]
Overall, the cert guide is pretty comprehensive on the subject of the conditional statements. One tiny insignificant detail. There is an example that goes like < ?php if ($a < 100) { echo 'Less than 100'; } else { echo 'More than 100'; } ?> Well, it’s evident that the second message should have been […]
The PHP certification guide is short but perfectly clear on the topic of postfix and prefix operations. My only remark is that due to the font used in this book, the pre/postfix decrement (–) is displayed as one long dash 🙁 Also the reader might be wondering if one can combine those operations, like: < […]
In this section, the curious reader might wonder about the combination of assignment by reference and a simple arithmetic operation such as addition. OK, let’s take a step back. Consider this: < ?php $a = 10; $b = $a; $a = 20; echo $b; ?> In this example the output will be 10, because the […]
I didn’t like the way WP is adding smart qoutes to my posts. This is especially annoying when you post programming code. So it had to go :-\
I couldn’t find an option to disable smart-quoting in the WordPress back-end so I took a look at the code and commented a few lines.