PHP Exception: When to use it?

I always write my code like this:

function name($arguments){
	(1) variables' declaration
	(2) variables' affectation from arguments by casting
	(3) variables' verification

	(4) do what I have to do

	(5) return value
}

But the step 2 and 3 are sometimes too much bigger, because I cast arguments in wanted type, and sometimes if I have a bad value I correct it.
Why I don’t just throw an exception if I saw an inconvenient type or value?
Continue reading

Substr or strncmp

What is the difference between substr and strncmp?

Summary

I was checking code and I was wondering what is better and faster between substr and strncmp, for checking same beginning string?
substr: Returns the portion of string specified by the start and length parameters.
strncmp: This function is similar to strcmp(), with the difference that you can specify the (upper limit of the) number of characters from each string to be used in the comparison.
Continue reading

Affectation: pre or post?

I used to write affectation’s code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< ?php
 
class test{
 
	private $integer;
 
	public function __construct($integer = 10){
		$integer = (int) $integer;
		if ($integer < 5) {
			$this->integer = 5;
		}
		else if ($integer > 15) {
			$this->integer = 15;
		}
		else {
			$this->integer = $integer;
		}
	}
}
 
$vld = new test(10);

But I don’t know if it’s better to affect in the last else, or before the if else statement?
Just see what’s happen when zend engine rendering this in opcode.
Continue reading