Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
Sunday, July 19, 2009
PHP: exceptions vs errors?
Was just reading twitts got the nice link about the exceptions handling the post basically explains about handling errors in PHP nice one check out the code how the fatal error are handled and customize message is displayed for user. I have not tested the code but looks ok :) . Click for post here
Wednesday, July 15, 2009
Debug message in PHP?
I am using PHP from past 4 year and used to debug my code using die and echo statement and will log the debug message in file if running backend job(Cron) while using PHP 4 and then we started using Zend Framework and use Zend_log. But for those who won't use Zend Framework here is the function which will be useful in debug debug_backtrace this function is available from PHP4 didn't know i just came to know while i was browsing php.net, This information is for user like me who didn't know about it :)
Monday, July 06, 2009
Install PHP 5.3 on WAMP Server
Just installed PHP 5.3.0 on WAMP, Nice addon facility in WAMP now can switch between 5.2.x and 5.3.0. It was so simple that even a person with little knowledge can do it.
First install WAMP.
Then download version of php you want and install from here.
Done.
Now go to the wamp server icon in tray and switch to the version you want :)
First install WAMP.
Then download version of php you want and install from here.
Done.
Now go to the wamp server icon in tray and switch to the version you want :)
Tuesday, June 02, 2009
include_path in php
I was just going through the post of Sam Hennessy about Zend Framework Location, I found a section which really surprised me about the include_path(performance). I know that what include_path is and how to modify it, i remember we used .htaccess way to change the include path in my previous organization, but didn't know about the performance effect if we didn't use it correctly, ok my old pal reading this post will surely correct it :)
What’s it look like?
A typical value of Linux include_path setting looks like:
.:/usr/share/php:/usr/share/pear
It breaks down into two parts, the path and the path separator.
The path separator is “:” on UNIX style systems and “;” on Windows and acts as a delimiter between multiple paths. You can also simple use the PHP constant PATH_SEPARATOR from within your PHP scripts.
You paths should be absolute and you should be careful not to add a trailing slash to the end. In the context of an include_path, a path of “.” is considered the current directory.
How do I change it?
The three common ways to add a new path to your include_path are, changing the php.ini, adding a value in an .htaccess file or from within a PHP script.
php.ini
Open your php.ini in a text editor and find “include_path” then add your Zend Framework path.
Before:
include_path=".:/usr/share/php:/usr/share/pear"
After:
include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"
.htaccess
In an .htaccess file, add:
php_value include_path ".:/usr/share/php:/usr/share/pear:/path/to/zf"
In a PHP File
Before you include any Zend Framework code add the following to your PHP code:
$path = '/path/to/zf'; set_include_path(get_include_path() .PATH_SEPARATOR. $path);
Performance
The earlier on in the include_path a path
you are intending to use occurs, the faster the lookup will be. So for performance reasons you should put the paths most used by your application at the beginning of the include_path
Let’s say Zend Framework is your applications most commonly used external library. We should then change our above example for php.ini file, from:
include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"
To:
include_path=".:/path/to/zf:/usr/share/php:/usr/share/pear"
This will reduce number of folders PHP has to check, and thus file system calls, by half.
I was under the impression that the performance gain from this optimization would only be minor if using an Op-code cache like APC or Zend Optimizer+. However Matthew O’Phinney informs me:
In the profiling and benchmarking I’ve done, it still makes a difference, as the opcode cache will first hit the realpath cache, which is when the path lookup will usually occur; once it determines the path, it then checks to see if it has opcodes for that path, and then merrily goes on its way. The sooner it finds a match, the faster it can identify and use the opcodes. That said, the performance difference is minor — you only notice it when you have many class files on any given request, and if you’re under heavy load. (And those were the conditions I was profiling.)
What’s it look like?
A typical value of Linux include_path setting looks like:
.:/usr/share/php:/usr/share/pear
It breaks down into two parts, the path and the path separator.
The path separator is “:” on UNIX style systems and “;” on Windows and acts as a delimiter between multiple paths. You can also simple use the PHP constant PATH_SEPARATOR from within your PHP scripts.
You paths should be absolute and you should be careful not to add a trailing slash to the end. In the context of an include_path, a path of “.” is considered the current directory.
How do I change it?
The three common ways to add a new path to your include_path are, changing the php.ini, adding a value in an .htaccess file or from within a PHP script.
php.ini
Open your php.ini in a text editor and find “include_path” then add your Zend Framework path.
Before:
include_path=".:/usr/share/php:/usr/share/pear"
After:
include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"
.htaccess
In an .htaccess file, add:
php_value include_path ".:/usr/share/php:/usr/share/pear:/path/to/zf"
In a PHP File
Before you include any Zend Framework code add the following to your PHP code:
$path = '/path/to/zf'; set_include_path(get_include_path() .PATH_SEPARATOR. $path);
Performance
The earlier on in the include_path a path
you are intending to use occurs, the faster the lookup will be. So for performance reasons you should put the paths most used by your application at the beginning of the include_path
Let’s say Zend Framework is your applications most commonly used external library. We should then change our above example for php.ini file, from:
include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"
To:
include_path=".:/path/to/zf:/usr/share/php:/usr/share/pear"
This will reduce number of folders PHP has to check, and thus file system calls, by half.
I was under the impression that the performance gain from this optimization would only be minor if using an Op-code cache like APC or Zend Optimizer+. However Matthew O’Phinney informs me:
In the profiling and benchmarking I’ve done, it still makes a difference, as the opcode cache will first hit the realpath cache, which is when the path lookup will usually occur; once it determines the path, it then checks to see if it has opcodes for that path, and then merrily goes on its way. The sooner it finds a match, the faster it can identify and use the opcodes. That said, the performance difference is minor — you only notice it when you have many class files on any given request, and if you’re under heavy load. (And those were the conditions I was profiling.)
Friday, May 29, 2009
Strategy pattern in PHP
Very nice post explaning strategy pattern, i don't have much idea about it, But reading the post i liked it. Also see the way he has used interface in php.
Wednesday, May 27, 2009
PHP Compiler Internals
Sebastian Bergmann explaining about php internal in the slides he goes in depth how the php code is compiled by zend engine and other step, if wanted to know how internally php code is worked on take a look at his slides
Tuesday, May 12, 2009
Search & replace in files using php
Very nice post from Sameer, in his blog code-diesel explaining how to search and replace in file using PEAR library File_SearchReplace.
Below are link explaining how to install PEAR.
Install PEAR on Windows
Install PEAR on LINUX
Read more about File_SearchReplace with example here
Below are link explaining how to install PEAR.
Install PEAR on Windows
Install PEAR on LINUX
Read more about File_SearchReplace with example here
Friday, May 08, 2009
PHP 5.3 upgrading notes
Hi all upgrading to php 5.3 just go through the php 5.3 upgrading notes on wiki.php.net, lots of changes done, lots of new classes,method also function added and extension added and removed. List of upgrading notes is huge go through it. Below are few quick notes:
Reserved word: GOTO,NAMESPACE,CLOSURE are now reserved word in PHP, Make correction to your programs or else php will trow Fatal Error while parsing the script.
Functions affecting backwards compatibility:
* var_dump() output with objects.
* session_start() now returns false if session startup fails for some reason.
* opendir(), scandir() and dir() now use the default context if no context passed.
* The new mysqlnd library necessitates using MySQL's newer 41 byte password format. Continued use of the old 16 byte passwords will cause mysql_connect() to produce the following error message: “mysqlnd cannot connect to MySQL 4.1+ using old authentication.” No need to worry if you are using MySQL version >= 4.1, Prior to MySQL 4.1, password hashes computed by the PASSWORD() function are 16 bytes long.
Bye Bye windows 98 or NT4: The minimum Windows versions are now windows 2000/XP.
Libraries added: mysqlnd (optional replacement for libmysql)
Extension Changes Causing Incompatibilities:
"Session" - Sessions would no longer succeed to store session-files in ”/tmp” path if open_basedir restriction is enabled, and ”/tmp” is not explicitly added to allowed paths list (special treatment of ”/tmp” was added in 5.2.2, but was not documented)
Deprecated:
* define_syslog_variables() now issues E_DEPRECATED
* all ereg functions now issues E_DEPRECATED (note that not all of them are prefixed with ereg)
New functions:
Array
* array_replace()
* array_replace_recursive()
Date
* date_add()
* date_sub()
* date_diff()
* date_parse_from_format()
* date_create_from_format()
* date_get_last_errors()
* timezone_version_get()
Filesystem
* parse_ini_string()
MySQLi
* mysqli_fetch_all()
* mysqli_get_connection_stats()
* mysqli_poll()
* mysqli_reap_async_query()
Reserved word: GOTO,NAMESPACE,CLOSURE are now reserved word in PHP, Make correction to your programs or else php will trow Fatal Error while parsing the script.
Functions affecting backwards compatibility:
* var_dump() output with objects.
* session_start() now returns false if session startup fails for some reason.
* opendir(), scandir() and dir() now use the default context if no context passed.
* The new mysqlnd library necessitates using MySQL's newer 41 byte password format. Continued use of the old 16 byte passwords will cause mysql_connect() to produce the following error message: “mysqlnd cannot connect to MySQL 4.1+ using old authentication.” No need to worry if you are using MySQL version >= 4.1, Prior to MySQL 4.1, password hashes computed by the PASSWORD() function are 16 bytes long.
Bye Bye windows 98 or NT4: The minimum Windows versions are now windows 2000/XP.
Libraries added: mysqlnd (optional replacement for libmysql)
Extension Changes Causing Incompatibilities:
"Session" - Sessions would no longer succeed to store session-files in ”/tmp” path if open_basedir restriction is enabled, and ”/tmp” is not explicitly added to allowed paths list (special treatment of ”/tmp” was added in 5.2.2, but was not documented)
Deprecated:
* define_syslog_variables() now issues E_DEPRECATED
* all ereg functions now issues E_DEPRECATED (note that not all of them are prefixed with ereg)
New functions:
Array
* array_replace()
* array_replace_recursive()
Date
* date_add()
* date_sub()
* date_diff()
* date_parse_from_format()
* date_create_from_format()
* date_get_last_errors()
* timezone_version_get()
Filesystem
* parse_ini_string()
MySQLi
* mysqli_fetch_all()
* mysqli_get_connection_stats()
* mysqli_poll()
* mysqli_reap_async_query()
Tuesday, May 05, 2009
Read file in php
Really nice post with example explaining how we can read large size file using php.
Retrieving original url from short url using PHP and CURL
Good post explaining how to get back original url from short url.
expanding short url to original url using PHP and CURL
expanding short url to original url using PHP and CURL
Monday, May 04, 2009
Checking coding standards with PHP_Codesniffer
PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP and JavaScript code to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.
I was really behind this in my previous company, i want to make sure everyone follows the coding standard, with this tool i am sure that problem is solved.
You can also define your own coding standard, it can be integrated with Subversion.
More detailed information with installation and example can be found here.
I was really behind this in my previous company, i want to make sure everyone follows the coding standard, with this tool i am sure that problem is solved.
You can also define your own coding standard, it can be integrated with Subversion.
More detailed information with installation and example can be found here.
Tuesday, April 28, 2009
PHP 5.3 features
I was just going through the interview of Lukas Kahwe Smith(Release manager php.net), He was explaining the key features we need to look out in PHP 5.3. In all the feature i am keen to use the feature lambda functions and closures and also PHAR.
About lambda function and closure i have already posted.
In this post lets look at PHAR, In the interview Lukas didn't explain in depth about it just mentioned PHAR is the result of a "proof of concept" PEAR package called "PHP_Archive".
I found an very good article on net about PHAR with an example.
About lambda function and closure i have already posted.
In this post lets look at PHAR, In the interview Lukas didn't explain in depth about it just mentioned PHAR is the result of a "proof of concept" PEAR package called "PHP_Archive".
I found an very good article on net about PHAR with an example.
Thursday, April 23, 2009
How to improve PHP session security
Nice article about session security, I never tried any of session hacking technique but its true as session id is stored in cookies and cookies are stored in user side and also it is passed to server directly anyone in between can sniff or hijack it :)
Go through the article how to protect it.
Go through the article how to protect it.
Tuesday, April 21, 2009
Useful PHP links
This list will be growing, please comment or send me a mail to add more link :)
i) Useful PHP Classes and Components
ii) Open-source PHP applications
iii) How to Install PHP on Windows
iv) A-Z PHP
v) Useful PHP + jQuery Components & Tuts for Everyday Project
vi) The ABC's of PHP
vii) Top 5 php template engines
viii) 10 Advanced PHP Tips To Improve Your Programming
ix) 6 books to master PHP
i) Useful PHP Classes and Components
ii) Open-source PHP applications
iii) How to Install PHP on Windows
iv) A-Z PHP
v) Useful PHP + jQuery Components & Tuts for Everyday Project
vi) The ABC's of PHP
vii) Top 5 php template engines
viii) 10 Advanced PHP Tips To Improve Your Programming
ix) 6 books to master PHP
PHP 5.3, Lambda Functions, and Closures
PHP 5.3 will have a lot of exciting new features. One of the most important one for me is the introduction of lambda functions and closures support.
For more information click
For more information click
Saturday, October 11, 2008
Filter Extension and Function in PHP
Was just going through the ppt and sample code of Tony Bibbs talk on about injection flaws, Cross Site Scripting (CSS) and Cross Site Request Forgeries (CSRF) given by him in Information Security Office (ISO), Came to know about useful Filter Function and Extension(This extension serves to validate and filter data coming from some insecure source, such as user input. ) in PHP.
Find the code and use of Filter function in action in below link
http://devzone.zend.com/node/view/id/1113
Find the documentation in below link
http://www.php.net/manual/en/intro.filter.php
Find the code and use of Filter function in action in below link
http://devzone.zend.com/node/view/id/1113
Find the documentation in below link
http://www.php.net/manual/en/intro.filter.php
Thursday, December 06, 2007
Becoming PHP 6 Compatible
In a new post on 'Making the Web blog', there's a new post that talks about preparing your code for PHP6 when it comes around. "If you want to make use of PHP 6 when it comes, you're going to have to write your new scripts so they are compatible, and possibly change some of your existing scripts. To start making your scripts PHP 6 compatible, I've compiled a list of tips to follow when scripting." The list of tips has five entries:
- Don't use register_globals
- Stop using magic_quotes
- Don't Register Long Arrays
- preg instead or ereg
- Don't initiate objects with the reference operator
Saturday, March 17, 2007
PHP Optimization Tricks
There are a number of tricks that you can use to squeeze the last bit of performance from your scripts. These tricks won't make your applications much faster, but can give you that little edge in performance you may be looking for.
1.When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't.
2.When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. But Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.
Ex.
if (strlen($foo) <> 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }
The bottom search mechanism is roughly 3 times faster.
9.file including and requiring.
First, you should only use require if you KNOW you will need that file on that page. If you MIGHT use it, use
include instead because PHP opens up all files that are required, whether the script gets there or not. To help out the programmer,
I suggest using "include_once" and "require_once".
Second, get your source file sizes as small as possible and only include/require files you absolutley must have.
If you only need one function in a file, consider breaking that function into its own file. I found on my system
that for every KB of source code that was included/required, I lost one transaction per second (tps). This may
not lead to perfectly organized code, but if speed is a concern, it is definitley worth it.
10.true is faster than TRUE
How come true is faster than TRUE?
This is because when looking for constants PHP does a hash lookup for name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups.
11.Perl-regexps are faster than POSIX-regexps
http://www.oreilly.com/catalog/regex/ This books explains regex and why a posix regex is slower.
12.FOR vs. WHILE vs. DO-WHILE
just out of curiosity I wanted to see which one of these structures are faster. I believed that "for" will be the fastest but the tests proved I was wrong.
for: 2.078712940216064
while: 1.981828927993774
do: 1.936743974685669
For 2,000,000 iterations the do-while structure is almost 0.15 seconds faster than the for structure. This won't mean very much for small sites but in complex application where you'll have to handle many hits it will make a difference.
13.Here are two tips for loops. If the number of iterations in a loop is low, you might get some performance gain from replacing the loop with a number of statements. For example, consider a for loop that sets 10 values in an array. You can replace the loop with 10 statements, which is a duplication of code, but may execute slightly faster.
Also, don't recompute values inside a loop ex:- count(arrname) .The price for coding in this style was a slight hit in performance, as the loop calls the count function repeatedly.
If you know or have any additional optimization tricks let me know.
References:-
http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
//Zend
1.When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't.
2.When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. But Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length.
Ex.
if (strlen($foo) <> 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }
The bottom search mechanism is roughly 3 times faster.
9.file including and requiring.
First, you should only use require if you KNOW you will need that file on that page. If you MIGHT use it, use
include instead because PHP opens up all files that are required, whether the script gets there or not. To help out the programmer,
I suggest using "include_once" and "require_once".
Second, get your source file sizes as small as possible and only include/require files you absolutley must have.
If you only need one function in a file, consider breaking that function into its own file. I found on my system
that for every KB of source code that was included/required, I lost one transaction per second (tps). This may
not lead to perfectly organized code, but if speed is a concern, it is definitley worth it.
10.true is faster than TRUE
How come true is faster than TRUE?
This is because when looking for constants PHP does a hash lookup for name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups.
11.Perl-regexps are faster than POSIX-regexps
http://www.oreilly.com/catalog/regex/ This books explains regex and why a posix regex is slower.
12.FOR vs. WHILE vs. DO-WHILE
just out of curiosity I wanted to see which one of these structures are faster. I believed that "for" will be the fastest but the tests proved I was wrong.
for: 2.078712940216064
while: 1.981828927993774
do: 1.936743974685669
For 2,000,000 iterations the do-while structure is almost 0.15 seconds faster than the for structure. This won't mean very much for small sites but in complex application where you'll have to handle many hits it will make a difference.
13.Here are two tips for loops. If the number of iterations in a loop is low, you might get some performance gain from replacing the loop with a number of statements. For example, consider a for loop that sets 10 values in an array. You can replace the loop with 10 statements, which is a duplication of code, but may execute slightly faster.
Also, don't recompute values inside a loop ex:- count(arrname) .The price for coding in this style was a slight hit in performance, as the loop calls the count function repeatedly.
If you know or have any additional optimization tricks let me know.
References:-
http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
//Zend
Subscribe to:
Posts (Atom)