Introduction
cPanel hosting environments come with a variety of PHP libraries pre-installed, making it easier for developers to utilize essential functionalities without additional setup. This guide outlines the default PHP libraries installed on most cPanel servers and provides examples of how to use them in your web applications.
Default PHP Libraries on cPanel Hosting Servers
By default, the following PHP modules are installed on cPanel hosting servers:
- bcmath
- hash
- mime_magic
- Reflection
- wddx
- calendar
- iconv
- mssql
- session
- xml
- ctype
- mysql
- SimpleXML
- xmlreader
- curl
- imap
- mysqli
- soap
- xmlrpc
- date
- ionCube Loader
- openssl
- sockets
- xmlwriter
- dom
- json
- pcre
- SourceGuardian
- xsl
- exif
- libxml
- PDO
- SPL
- zip
- filter
- magickwand
- pdo_mysql
- SQLite
- zlib
- pspell
- tokenizer
- ftp
- mbstring
- pdo_sqlite
- standard
- gd
- mcrypt
- posix
- tidy
- gettext
- mhash
- GMP (GNU Multiple Precision)
Using Pre-Installed PHP Libraries
1. bcmath
BCMath provides arbitrary precision mathematics. It’s useful for applications that require high precision calculations.
Usage Example:
echo bcadd('1.234', '5.678', 2); // Outputs: 6.91
#2. curl
cURL is a library for making HTTP requests. It is widely used for fetching data from APIs.
Usage Example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
#3. gd
GD is a graphics library used for creating and manipulating images.
Usage Example:
header('Content-Type: image/png');
$im = imagecreatetruecolor(100, 100);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 1, 5, 5, 'Hello World', $textcolor);
imagepng($im);
imagedestroy($im);
#4. mbstring
mbstring provides multibyte string functions, useful for handling non-ASCII characters.
Usage Example:
$str = "こんにちは";
echo mb_strlen($str); // Outputs: 5
#5. PDO
PDO provides a consistent interface for accessing databases. It supports multiple databases like MySQL, PostgreSQL, and SQLite.
Usage Example:
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'root';
$password = '';
$options = [];
$pdo = new PDO($dsn, $username, $password, $options);
$stmt = $pdo->query('SELECT * FROM users');
while ($row = $stmt->fetch()) {
echo $row['name'] . "<br>";
}
#6. SimpleXML
SimpleXML is used for parsing XML data easily.
Usage Example:
$xmlString = '<root><item>Hello</item></root>';
$xml = simplexml_load_string($xmlString);
echo $xml->item; // Outputs: Hello
#7. json
The JSON extension is used for encoding and decoding JSON data.
Usage Example:
$array = ['name' => 'John', 'age' => 30];
$json = json_encode($array);
echo $json; // Outputs: {"name":"John","age":30}
$jsonString = '{"name":"John","age":30}';
$array = json_decode($jsonString, true);
print_r($array); // Outputs: Array ( [name] => John [age] => 30 )
#8. zip
The ZIP extension is used for reading and writing ZIP archives.
Usage Example:
$zip = new ZipArchive();
$filename = "./test.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfile.txt", "file content");
$zip->close();
Conclusion
The default PHP libraries installed on cPanel hosting servers provide a wide range of functionalities that can be utilized in your web applications. By understanding and leveraging these libraries, you can enhance the capabilities of your projects without the need for additional installations.
For further assistance, you can refer to Domain India's Knowledgebase or submit a support ticket at Domain India's Support.