AS said i will add some more tips & tricks
### CLASS TIPS ###
E_STRICT is an added error_reporting option in PHP5, but what does it do ?
Copy a class tip example of the first post and put it into a PHP file and add an additional line.
PHP Code:
<?php
error_reporting(E_ALL | E_STRICT); # <= added line
class this_test
{
public function echo()
{
$this->foo();
}
public function echo_dyn()
{
this_test::foo();
}
public function foo()
{
echo 'foo';
}
}
$test = new this_test();
$test->echo();
this_test::echo();
this_test::echo_dyn();
You will notice that the page is showing 'Runtime Notices' like:
Non-static method this_test::echo() should not be called statically
If you want to call functions inside a class staticly (class::function) besides dynamicly ($this->function) you only have to add the keyword 'static' to the function identifier.
PHP Code:
error_reporting(E_ALL | E_STRICT);
class this_test
{
public static function echo()
{
$this->foo();
}
public static function echo_dyn()
{
this_test::foo();
}
public static function foo()
{
echo 'foo';
}
}
$test = new this_test();
$test->echo();
this_test::echo();
this_test::echo_dyn();
### STRING CONTENT ###
There are several ways to compare the contents of a string like ereg, eregi, preg_match and strpos.
ereg is similar to preg_match which uses regular expressions and eregi is the case-insensitive way. Although ereg is slower then preg_match it is easier if you don't understand regular expressions very well.
But the underdog is strpos and in PHP5 the stripos (case-insensitive) which are actualy the fastest compare functions for normal strings.
PHP Code:
$haystack = 'I love CodeNewbie.com';
$needle = 'love';
strpos($haystack, $needle); # case-sensitive
stripos($haystack, $needle); # PHP 5 case-insensitive
NOTE: Do keep in mind that strpos could return 0 since 0 is the first character.
So if you want to check if needle exists inside a string then use the triple =
PHP Code:
if (stripos($haystack, $needle) !== false) {
echo "$needle found";
}