Friday, 2 September 2011

PHP Blog World: JavaScript Libraries | PHP Development Blog | PHP Frameworks | Latest News | PHP Content Management System

What's this, no multi-page post today—complete with code examples and links all over the place? Instead, I did some house cleaning, and added a new wing. JavaScript has been such a hot topic around here of late, and in the Web development community in general, that I took a look at my growing list of resources and discovered that indeed, the JavaScript category was in need of some pruning. Amongst the list, and in particular by studying the most popular tags, I found that JavaScript libraries would make an excellent category to splinter off and reduce the weight in the parent folder. I know, I know, I need to implement paging. PHP is another one that grew out of its clothes.

Speaking of implementing paging (and caching and a lot of other things on my todo list), I have been really busy behind the scenes fixing bugs and making other improvements to both this blog and loadaverageZero in general. Some of them my visitors may have noticed, some may not. But things are definitely on the upswing around here since health has improved.

One thing that really has me puzzled is what to do with this blog. I'm running a rather old, and hacked to pieces, version of Serendipity. I don't want to let go of the design, which I spent a lot of time on, yet on the same token it bothers me that my only recourse to block spammers and other miscreants was to disable comments and trackbacks. I'm not a big fan of Captchas, I would prefer to go the OpenID route.
Share/Bookmark

PHP Blog World: Get Facebook Stream On Your Google Plus | PHP Development Blog | PHP Frameworks | Latest News | PHP Content Management System


STEPS

* Sign In Your Google Plus
* Open a New Browser Window Or Tab
* Then Type This URL www.crossrider.com/install/519-google-facebook Or Click Here
* Click on Get Google+Facebook And Install Plugin
* Then restart Your Browser And Open Google Plus Facebook Icon Will Appear in Top Bar
* Click and Connect to Facebook



Share/Bookmark

php source code,php example, coding,PHP5,PHP script,Free PHP example,Free PHP Script,Free: How to get unique value form PHP array?


array_unique()


(PHP 4 >= 4.0.1, PHP 5)

array_unique — Removes duplicate values from an array

Description:

Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

Example:


/*= array("a" => "1", "2", "3" => "1", "2", "3");
$result = array_unique($input);
print_r($result);
*/

Result:
/*Array
(
[a] => 1
[0] => 2
[2] => 3
)*/



PHP How to: Assignment Operators

The main assignment operator is = which is used to assing the right side value to the left. It doesn't mean 'equal to' as in maths. It means 'set to' . This operator returns the result of the assignment.

$x=18;

This statement has the value 18 . A better example:

$x= ($y=3) + ($z=$y-1) ;

This statement assigns 3 to $y, substracts 1 from $y and assigns it to $z and then adds them to assign the result to $x . There are some combined assignment operators too:

operator example equivalent

+= $x+=$y $x=$x+$y
-= $x-=$y $x=$x-$y
*= $x*=$y $x=$x*$y
/= $x/=$y $x=$x/$y
%= $x%=$y $x=$x%$y
.= $x.=$y $x=$x.$y

PHP Try Catch Guide

This example demonstrates the drawbacks of returning null from your methods. I allays preach to my colleagues that when something is wrong, your method should throw an exception instead of returning a null.
But sometimes, especially if I'm writing some tools for myself, because of the laziness, I use the return null too.

Few days ago I done that, and very soon I released that it was mistake which caused me unnecessary problems.

Story goes like this: There is an initial list of ids and result of a process is a array of fully populated objects. I use that procedure on two places in my tool: for generating RSS file and for generating a view script. The tool is written in Zend Framework, it uses a log file, when exception occurs displays a customized error page and sends a email to me.

Initially it was all flawless, but at some point, I released that I'm not able to read data for some of initial ids. So I modified a loading method in something like this:

public function loadItem( $id)  {   $raw_data = $this->_getRawData( $id);   if ($this->_testRawData( $raw_data)    return $this->_parseData( $raw_data);   return null;  } 

Accordingly, I modified the controller part to:

public function rssAction()  {    $ids = $this->getRssIds();    $items = array();    foreach ($ids as $id)    {      $item = $this->manager->loadItem( $id);      if ($item)       $items[] = $item;    }    $this->view->items = $items;  } 

I done it, tested the RSS feature and it was as expected, all OK. I've done few more fixes, and at last, after some time, I wanted to see the preview too.
Cold shower. PHP notices, warnings, fatal error. Undefined variable, call to a method on null object.... Why, how, what a hell ...?
Standard debugging procedure: I'm checking the log file. Nothing to find there. Next is to check the view script and associated action. Soon I found that the problem was that I had null values in array which supposed to be populated with objects. I simply forgot that I was using the same method on two places.

public function previewAction()  {    $ids = $this->getPreviewIds();    $items = array();    foreach ($ids as $id)       $items[] = $this->manager->loadItem( $id);     $this->view->items = $items;  } 

At the end, as I'm experienced developer and this was my "in house" tool, I found and fixed it very quickly (still bad solution: again I put a test if the returned object is null), but it raised a really good question: When you are doing a bigger scale project, where you can not have all the things in your head, what is the right solution and what would be the consequences?

First the correct code variant:

// somewhwere in my manager class  public function loadItem( $id)  {   $raw_data = $this->_getRawData( $id);   if ($this->_testRawData( $raw_data)    return $this->_parseData( $raw_data);   throw new ItemNotFoundException( 'Item ['.$id.'] could not be loaded');  }   // somewhwere in my controller class  public function rssAction()  {    $ids = $this->getRssIds();    $items = array();    foreach ($ids as $id)    {       try       {        $items[] = $this->manager->loadItem( $id);       }       catch (ItemNotFoundException $e)       {       }    }    $this->view->items = $items;  }  

Let's assume that I still miss to upgrade the previewAction() method. Now, instead of bunch of warnings, I'll have Exception. This Exception is important because
1. Debugging: It will be stored to my log file, and in the development/testing process I'll quickly determine where is the problem and what I have to fix (Front controller handles it)
2. Application consistency: If I forgot to test any other part of my application which might use that manager method, and I put that code on live installation, users will encounter the customized error screen, error will be logged, and I'll receive the alarm email. The error will not be able to pass unnoticed!