“It's not about how to achieve your dreams, it's about how to lead your life, ... If you lead your life the right way, the karma will take care of itself, the dreams will come to you.” ― Randy Pausch, The Last Lecture
Friday, March 23, 2012
Basics of PHP strings manupulations
I feel that people should know how PHP act on strings manupulations and what are major superfine things that we know.
1. switch vs if vs if-else
$var=rand(0,1000000);
if ($var==0)
{
echo "found 0";
}
elseif ($var==1)
{
echo "found 1";
}
elseif ($var==2)
{
echo "found 2";
}
....
$var=rand(0,1000000);
if ($var==1)
{
echo "found 1";
}
if ($var==2)
{
echo "found 2";
}
if ($var==3)
{
echo "found 3";
}
....
$var=rand(0,1000000);
switch ($var)
{
case 1:
echo "found 1";
break;
case 2:
echo "found 2";
break;
case 3:
echo "found 3";
break;
....
However, in this case, many switch statements still performed faster (around 25% more) than when using many if-else or if,if conditionals instead. This was tested with 100,000 iterations of 10 conditions as opposed to a 1 iteration of 1,000,000 conditions.
2. isset() vs array_key_exists()
$arr = array(); $fp = fopen("/usr/share/dict/words", "r"); while ($i < 5000 && ($w = fgets($fp))) {
$arr[trim($w)] = ++$i; } $s = microtime(1); for ($i = 0; $i < 100000; $i++) { isset($arr['abracadabra']); } $e = microtime(1); echo "Isset: ".($e - $s)."\n"; $s = microtime(1); for ($i = 0; $i < 100000; $i++) { array_key_exists('abracadabra', $arr); } $e = microtime(1); echo "array_key_exists: ".($e - $s)."\n";
shows that while isset() is 2.5 times faster taking a mere 0.0219 seconds vs 0.0549 seconds taken by array_key_exists(), both operations are extremely quick and take a fraction of a second even when doing 100k operations. Any "optimization" between the calls is purely within the super-micro-micro optimization realm and is not going to make any application measurably faster.
3. in_array vs isset
foreach($arr1 as $key=>$i){
if(in_array($i, $arr2)){
unset($arr1[$key]);
}}
That was running for hours with about 400k items.
foreach($arr1 as $key=>$i){
if(isset($arr2[$i])){
unset($arr1[$key]);
}
}
Yeah, that runs in .8 seconds. Much better. FWIW, I tried array_diff() as well. It took 25 seconds.
4. Null vs. isset()
Most intresting and most confusing ... lets start from begniing ..
$a = "0"; // set zero as string value
if(isset($a)) // it will return TRUE BUT
if($a) // it will return FALSE
$null = null;
if($null == null){
echo "Yes, it is equivilent to null\n";
}
if($null === null){
echo "Yes, it is null\n";
}
if(isset($null)){
echo "Yes, it is set\n";
}
if(empty($null)){
echo "Yes, it is empty\n";
}
However some people can ask Why not using is_null? http://www.php.net/is_null
Keeping track of the different comparison operators can be confusing.
I often find myself referring to this chart :
http://www.php.net/manual/en/types.comparisons.php
To check if the variable doesn't exist and to not be confused with null, you may use :-
- for key of array : array_key_exists($key, $array)
- for property of object : property_exists($property)
- for global variable : array_key_exists('variable', $GLOBALS)
- for local variable : you could test against the array returned by get_defined_vars() function like this:
array_key_exists('foo', get_defined_vars());
5. Semicolon error keep your eyes open !!!
$i = 0;
while($i < 20); {
//some code here
$i++;
}
See that semicolon there? This will cause PHP to silently fall into an infinite loop. You will not get any errors even with E_ALL|E_STRICT error levels – beware of this mistake. The missing semicolon after the break in the inner loop will cause the code to only output “0″ and then exit.
Hope you enjoy post. Good luck. Happy coding...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment