PHP Code Profiling
For a block of code if you want to profile it to check how long each statement takes to execute just use the PEAR Benchmark module.
require 'Benchmark/Timer.php';
$timer =& new Benchmark_Timer(true);
$timer->start();
// some setup code here
$timer->setMarker('setup');
// some more code executed here
$timer->setMarker('middle');
// even yet still more code here
$timer->setmarker('done');
// and a last bit of code here
$timer->stop();
$timer->display();
Explanation:
Calling setMarker( ) will records the time. The display( ) method prints out a list of markers, the time when they were set, and elapsed time from previous marker:
-------------------------------------------------------------
marker | time index | ex time | perct
-------------------------------------------------------------
Start | 1029433375.42507400 | - | 0.00%
-------------------------------------------------------------
- Kiran's blog
- Add new comment
- Read more
- 274 reads
Compress Web Output using gzip in PHP
For sending compressed content to browsers that support automatic decompression just add the following setting to your php.ini file:
zlib.output_compression=1
Browsers just tell the server that it can accept compressed responses with the Accept-Encoding header. If a browser sends Accept-Encoding: gzip or Accept-Encoding:deflate, and your PHP is built with the zlib extension, the zlib.output_compression
configuration directive then inform PHP to compress the output with the appropriate algorithm before sending it back to the browser. The browser uncompresses the data before displaying it.
You can adjust the compression level with the zlib.output_compression_level configuration directive:
; minimal compression zlib.output_compression_level=1 ; maximal compression zlib.output_compression_level=9
- Kiran's blog
- Add new comment
- Read more
- 436 reads
Split Filename into its Component Parts in PHP
Incase you need to find a file's path and filename, use basename( ) to get the filename and dirname( ) to get the path.
$full_filename = '/usr/local/php/php.ini'; $base = basename($full_name); // $base is php.ini $dir = dirname($full_name); // $dir is /usr/local/php
You can aslo try pathinfo( ) to get the directory name, base name, and extension into an associative array.
For a practical example, to create a temporary file in the same directory where the existing file is, use dirname( ) to
find the directory, and pass that directory to tempnam( ).
$dir = dirname($existing_file); $temp = tempnam($dir,'temp'); $temp_fh = fopen($temp,'w');
The elements in the associative array returned by pathinfo( ) are dirname, basename, and extension:
$info = pathinfo('/usr/local/php/php.ini');
print_r($info);
This will give the output:
Array
(
[dirname] => /usr/local/php
[basename] => php.ini
- Kiran's blog
- Add new comment
- Read more
- 388 reads
Load Extensions Dynamically in PHP
It is possible to load extension libraries at runtime, if they are not compiled in, using the dl() function.
dl() function expects as a parameter the name of the file containing the library. IN UNIX, it will be filenames ending in .so; and in Windows, filenmames will end in .dll.
An example of a call to dl() is
dl(“php_ftp.dll”);
The above code will dynamically load the FTP extension (on a Windows machine).
You need not specify the directory where the file lives: Instead, you should configure this in the php.ini file. A directive called extension_dir will specify the directory where PHP will
look for libraries to dynamically load.
If you find you are having trouble dynamically loading extensions, then check your php.ini file for the enable_dl directive. If it’s off, you won’t be able to dynamically load extensions.
- Kiran's blog
- Add new comment
- Read more
- 697 reads
Evaluating Strings In PHP
In PHP the function eval() will evaluate a string as a PHP code.
For example,
eval ( "echo 'Hello Name';" );
in the above code eval will take the contents of the string and execute it and hence will produce the same output as
echo 'Hello Name';
There may be lot of cases when eval() can be useful. You can store blocks of code in a database, and retrieve and eval() them at a later point. You can generate code in a loop, and then use eval() to execute it. You can safely use eval() to update or correct existing code.
If you had a large collection of scripts that needed a predictable change, it would be possible (but inefficient) to write a script that loads an old script into a string, runs a regexp to make changes, and then uses eval() to execute the modified script.
It is even conceivable that a very trusting person somewhere might want to allow PHP code to be entered in a browser and executed on her server.
- Kiran's blog
- Add new comment
- 204 reads













