Google

Saturday, January 23, 2010

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
)*/



Saturday, July 25, 2009

Read CSV file through PHP

Read CSV file through PHP

/*

$file_handles = fopen("my.csv", "r");

while (!feof($file_handles) ) {

$line_of_texts = fgetcsv($file_handles, 1024);

print $line_of_texts[0] . $line_of_texts[1]. $line_of_texts[2] . "
";

}

fclose($file_handle);

*/

Saturday, July 11, 2009

Difference between Cookies and Session (Cookies vs sessions) in PHP

Cookies and Session (Cookies vs sessions) in PHP

Both are use in terms of data storage in programming language...but how ? what is the preferable?, when use session?, when use cookie?, there are some question which arise in the mind at the time of of store data temp. as well as permanent.

There are differences between the two that will make each favorable in their own circumstance.

Cookies can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies, having their data stored on the client, work smoothly when you have a cluster of web servers, whereas sessions are stored on the server, meaning if one of your web servers handles the first request, the other web servers in your cluster will not have the stored information.

Sessions are stored on the server, which means clients do not have access to the information you store about them – this is particularly important if you store shopping baskets or other information you do not want your visitors to be able to edit by hand by hacking their cookies. Session data, being stored on your server, does not need to be transmitted with each page; clients just need to send an ID and the data is loaded from the local file. Finally, sessions can be any size you want because they are held on your server, whereas many web browsers have a limit on how big cookies can be to stop rogue web sites chewing up gigabytes of data with meaningless cookie information.

So, as you can see, each have their own advantages, but at the end of the day it usually comes down to one choice: do you want your data to work when you visitor comes back the next day? If so, then your only choice is cookies – if you have any particularly sensitive information, your best bet is to store it in a database, then use the cookie to store an ID number to reference the data. If you do not need semi-permanent data, then sessions are generally preferred, as they are a little easier to use, do not require their data to be sent in entirety with each page, and are also cleaned up as soon as your visitor closes their web browser.

Friday, June 26, 2009

PHP Issue Trackers software list

Mantis


* Web: http://www.mantisbt.org/
* Requirements: PHP 4.3
* Supported databases are MySQL with stated support for Postgress, MSSQL, DB2 and Oracle.
* License: GPL


Eventum

* Web: http://dev.mysql.com/downloads/other/eventum/
* Requirements: PHP 5.1
* Supported databases is MySQL.
* License: GPL


Bugs the Bug genie

* Web: http://www.thebuggenie.com/
* Requirements: PHP 5.1.
* Supported databases is MySQL.
* License: MPL 1.1


WHUPS

* Web: http://www.horde.org/whups/
* Requirements: Horder framework 3.2+. PHP 4.3+.
* Most databases are supported.
* License: BSD license


phpBugTracker

* Web: http://phpbt.sourceforge.net/
* Requirements: PHP 4.0.6+.
* Supported databases are MySQL, PostreSQL and Oracle.
* License: BSD license


Other PHP Systems

* Flyspray - http://flyspray.org/
* FireAnt - http://chaozz.nl/software/fireant/
* zenTrack - http://www.zentrack.net

PHP performance tips by google

1) Upgrade your version of PHP

2) Use caching

3) Use output buffering

4) Don't copy variables for no reason.

5) Avoid doing SQL queries within a loop

6) Use single-quotes for long strings.

7) Use switch/case instead of if/else.

Saturday, May 23, 2009

How to read integer value from XML in PHP and after read add those value

When you read XML in php through simple xml class then at the time of integer value you need to type cast those value in "string", because
when you read through simple xml class object then he return the object(show through the var_dump).

Ex:

your XML look like -










now you want to read both ints tag value and add both value.


/*


$xml->load('abc.xml');

foreach($xml->a[0] as $abTagObj)
{

$ints[] = (String)$abTagObj->ints;
}

array_sum($ints); // show the total of all ints tag.

*/


but if you not type cast ints tag value as "string" then when you go for add then show the total as 0.


If still any question then pleases put your comment i will answer you. or any question regarding PHP, javascript, ajax, css, html, mysql put your comment feel free.

Saturday, April 25, 2009

Memcache for Code and Performance

"Memcache for Code and Performance "



When website has high traffic then continuos every time fetch record from db and show on the client side is tedious job and we need high performance then we use Memcache.



What is Memcache?



"memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."



What is MemcacheDB?



It's a distributed key-value storage system designed for persistent. It is NOT a cache solution, but a persistent storage engine for fast and reliable key-value based object storage and retrieval. It conforms to memcache protocol(not completed, see below), so any memcached client can have connectivity with it. MemcacheDB uses Berkeley DB as a storing backend, so lots of features including transaction and replication are supported.



Before you get too excited keep in mind this is a key-value store. You read and write records by a single key. There aren't multiple indexes and there's no SQL. That's why it can be so fast.



The original client for memcached was written in Perl by the folks at Danga Interactive. The Python client is now maintained by a 3rd party group. The PHP client is available as a PECL package.PHP client is written in C as a PHP extension. The other two are written in Perl and Python. So, all the complex code has to be interpretted. For PHP however, most of the work happens in the engine, not in PHP code. This makes the PHP client far superior in performance to the other two. By default, memcached uses the port 11211.



The system is used by several very large, well-known sites including YouTube, LiveJournal, Slashdot, Wikipedia, SourceForge, ShowClix, GameFAQs, Facebook, Digg, Twitter, Fotolog, BoardGameGeek, NYTimes.com, deviantART, Jamendo, Kayak and in our Mobshare too.




PHP provides support for the Memcache functions through a PECL extension. To enable the PHP memcache extensions, you must build PHP using the --enable-memcache option to configure when building from source.



A few important things about memcache:



Memcache is a daemon, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to connect to it, just like MySQL.



Think of memcache as the $_SESSION variable for PHP, but instead of it working on a per-user basis, it runs over the entire application — like MySQL. In fact, you can use memcache as you session handler for PHP.




Memcache to work on windows machine:



Download memcache [grab the 'win32 binary' version]



Install memcache as a service:



  • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] - thanks to Stephen for the heads up on the new version

  • If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.

  • Install the service using the command: c:\memcached\memcached.exe -d install from the command line

  • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server"



Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.



1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
If you don’t have any luck finding it, try looking here: pecl4win.php.net/ext.php/php_memcache.dll [or look here for PHP 5.2.*]



2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:



extension=php_memcache.dll



3. Restart apache



4. Run this code to test the installation:



/*

$memcache = new Memcache;
$memcache->connect("localhost",11211);

"Server's version: " . $memcache->getVersion();

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

$memcache->set("key",$tmp_object,false,10);
echo "Store data in the cache (data will expire in 10 seconds)";

echo "Data from the cache:";
var_dump($memcache->get("key"));
*/

If you see anything but errors, you are now using memcache!



EDIT



Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:


“C:\memcached\memcached.exe” -d runservice -m 512



Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.



Memcache Functions:

The memcache object has several methods. With the Memcache::set method we can add an entry to the cache. The method had four arguments:

  • a key

  • any serializable variable we'd like to cache, in this case it's a string

  • a boolean whether we want to use on-the-fly zip compression using libz

  • the cache expiry measured in seconds

  • Ex: - $memcache->set("key",$tmp_object,false,10);
  • Memcache::add — Add an item to the server

  • Memcache::addServer — Add a memcached server to connection pool

  • Memcache::close — Close memcached server connection

  • Memcache::connect — Open memcached server connection

  • memcache_debug — Turn debug output on/off

  • Memcache::decrement — Decrement item's value

  • Memcache::delete — Delete item from the server

  • Memcache::flush — Flush all existing items at the server

  • Memcache::get — Retrieve item from the server

  • Memcache::getExtendedStats — Get statistics from all servers in pool

  • Memcache::getServerStatus — Returns server status

  • Memcache::getStats — Get statistics of the server

  • Memcache::getVersion — Return version of the server

  • Memcache::increment — Increment item's value

  • Memcache::pconnect — Open memcached server persistent connection

  • Memcache::replace — Replace value of the existing item

  • Memcache::setCompressThreshold — Enable automatic compression of large values

  • Memcache::setServerParams — Changes server parameters and status at runtime





The data is converted into a string with the serialize() function, so numbers, strings, arrays, and most objects will be stored easily… only resources (file descriptors, database connections, MemCache connections) can not be stored effectively.




It is important to keep in mind that if the MemCached server crashes or the process dies, all your cache data will be lost. Always store the actual data in a database server or another persistent storage system.




Conclusion



While the example we created is fairly trivial, it’s easy to see how this technique can be expanded throughout your site. Other queries and other data can be cached with similar benefits. Obviously, MemCached isn’t the only solution to web scaling and data caching, but it can be a pretty useful. Often, as seen in our example, a MemCached solution can be implemented and put in place with minimal code changes, minimal time, and, hopefully, minimum downtime for your users!