20 Great PHP Libraries You Need to Know

You’re in the middle of a project, and need to get everything done as soon as possible. To keep the deadline and still have a life, you would better to collect this awesome library on your fingertip. By knowing this library capability, then you’ll know when to use any of them.

Charting Library

You can create simple graph or chart using GD library on PHP, but to help you create more complex chart, then you’ll need this awesome library.

  1. pChart – a PHP class to build charts.
  2. Libchart – Simple PHP chart drawing library.
  3. JpGraph – Object-oriented graph creating library for PHP.
  4. Open Flash Chart – Flash based charting library.

RSS Parser Library

Parsing a RSS is not a fun thing to do, so you would better put this library and get everything done.

  1. MagpieRSS – RSS for PHP.
  2. SimplePie – Super-fast, easy-to-use, RSS and Atom feed parsing in PHP.

Thumbnail Generator

Just another way to create thumbnail.

  1. phpThumb – The PHP thumbnail creator.


Dealing with an e-commerce site? Need payment solution? Don’t worry. Let this library help you.

  1. PHP Payment Library – PHP Payment Library for Paypal, Authorize.net and 2Checkout (2CO)


Just in case you need to implement OpenID to your project.

  1. PHP-OpenID – OpenID implementation in PHP.

Database Abstraction/ORM

Writing SQL Query isn’t really exciting. These library let you smile more.

  1. ADOdb – Database abstraction library for PHP.
  2. Doctrine – Object relational mapper (ORM) for PHP 5.2.3+ that sits on top of a powerful database abstraction layer (DBAL).
  3. Propel – Object-Relational Mapping framework for PHP5
  4. Outlet – an Open source object-to-relational mapping tool for PHP.

PDF Generator

  1. FPDF – PHP class which allows to generate PDF files with pure PHP.


Need to generate your report in Excel file. Well, you better have this library on your side.

  1. php-excel – Very simple library for generating excel documents from php on-the-fly.
  2. PHP Excel Reader – Parse and retrieve information from XLS files.


Don’t like PHP mail function? Put this library on your project, and you have more features when dealing with email.

  1. Swift Mailer – Free Feature-rich PHP Mailer.
  2. PHPMailer – Powerful email transport class with a big features and small footprint.

Unit Testing

For Test-driven development lover, these library will help you get your heart at home.

  1. SimpleTest – PHP unit test and web test framework.
  2. PHPUnit – Member of the xUnit family of testing frameworks and provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results.



(1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static 变量。
(2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。(不要用return 语句返回指向“栈内存”的指针,因为该内存在函数结束时自动消亡)

char *GetString(void)
char p[] = “hello world”;
return p; // 编译器将提出警告

void Test4(void)
char *str = NULL;
str = GetString(); // str 的内容是垃圾
cout<< str << endl;


char *GetString2(void)
char *p = “hello world”;
return p;
void Test5(void)
char *str = NULL;
str = GetString2();
cout<< str << endl;

但这种设计概念却是错误的。因为GetString2 内的“hello world”是常量字符串,位于静态存储区,它在程序生命期内恒定不变。无论什么时候调用GetString2,它返回的始终是同一个“只读”的内存块。
(3) 从堆上分配,亦称动态内存分配。程序在运行的时候用malloc 或new 申请任意多少的内存,程序员自己负责在何时用free 或delete 释放内存。动态内存的生存期由我们决定,使用非常灵活,但问题也最多。



1. 创建引用时必须初始化,而且不能初始化为null;  而指针可以不初始化,也可以初始化为null

int &a; //error, references must be initialized
int *p; //right
int &a = NULL; //error, can’t init as NULL
int *p = NULL; //right

2. 引用初始化后,就不再改变; 而指针随时可以改变所指的对象。

int a = 1;
int b = 2;
int &r = a; //r成为a的引用,即别名,r恒为a 的引用,不可改变
r = b;//此时不要以为r变成了b的引用,而是把b的值赋给了a, 此是a, r 都为b的值2
r = 3;//a, r的值都变成了3,b的值不变,仍为2


int a = 1;
int b = 2;
int *p = &a; // *p == 1, p初始化成了a的地址值
p = &b; // *p == 2,p地址值变了,p变成了b的地址值











c语言里的sizeof 是一个计算数据存储空间大小的单目运算符,它返回数据所占的字节个数, 我们可以先来看看MSND里的定义:

sizeof Operator
sizeof expression

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).

When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment.
When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of
dynamically allocated arrays or external arrays


1:sizeof运算符可以得到变量和类型存储空间的大小,大小是以字节为单位的,如一个变量a所占的空间是2个字节,那么sizeof(a) == 2。

2:如果a是一个数据类型,那么必须加括号表示为sizeof(a)形式,如果a是变量sizeof a也是可以的,但通常都写成sizeof(a)形式。

3:当sizeof 应用于结构体或变量时,它返回实际所占的空间大小,这个空间可能包括编绎器为了内存对齐而插入的额外的字节。

4:当sizeof应用于静态多维数组时,它返回整个数据的大小,但sizeof 不能应用于对态分配的数组。


1. 用于类型

sizeof(int) //返回4,表示整形数占4个字节,其实这个与系统有关, 一般linux和32位windows都返回4;

sizeof(char) //返回1;

struct student {
int age;
char name[20];
sizeof(student);// 返回24;

struct student {
int age;
char sex;
sizeof(student);// 返回8, 这里为什么会返回8呢,一个int占4个字节, 一个char占一个字节, 应该是5个字节啊, 其实不这样的, 编绎器为了更高效率而使得内存对齐, 为char sex另外插入了3个字节, 所以返回8;

2. 用于变量

int a = 100;

sizeof(a);//返回4, 一般根据它的类型而决定它所占空间大小

long float f = 33.00;

sizeof(f );//返回8,

char *p;

sizeof(p );//返回4, 这里p是一个指针变量,sizeof(p)返回这个指针变量所占的空间大小, 而不是字符p所占的空间大小(一个字符只占一个字节)。

3. 用于数组

int int_arr[5];
sizeof(int_arr);//返回20, 而不是5,当用于数组时, 它返回这个数组所点的总字节数, 而不是数组无素的个数;