Posts tagged PHP
PHP – Array – Shuffle() gotcha
0Be careful when using PHP’s shuffle() function, it will reset your keys. Why does this matter? What if you had an associative array? Or, if you populate arrays with DB data and set an array key to the primary key of a db row.
There are some user contributed functions which will maintain existing keys, but IMO PHP dev team should add a MAINTAIN_KEYS flag or something so it can be used on the source level and not the PHP level.
News – Software Behind Facebook
0http://royal.pingdom.com/2010/06/18/the-software-behind-facebook/ wrote an awesome article about the software used to support Facebooks immense online success. It offers great insight into which solutions you should consider for your own projects to ensure a highly optimized web presence.
Enjoy.
PHP – Compilation – Make Clean
0Came across an issue while I was installing XDebug on my development server. I wanted to add xdebug specific params to a seperate .ini file saved as xdebug.ini (vs. adding to tail-end of php.ini file). To support this I had to make php scan additional ini directories during httpd start-up, so I added ‘–with-config-file-scan-dir=/some/path’ to the configure command.
Well after executing make and make install commands, I would loadup a phpinfo.php script which simply echoed phpinfo(), and noticed that the “Scan this dir for additional .ini files” directive was empty. I was confused about this, because I explicitly added a path to the configure command.
After spending some time checking stuff out, I realized I forgot to execute ‘make clean’, which purges out all the previous compilation’s temp make files etc. So I executed the following commands:
make clean make make install
With the added ‘make clean’ command everything worked as expected when I reloaded the phpinfo() script.
News – 4000% Performance Increase by Sorting in PHP vs MySQL
0A buddy at work sent out the following link which proved to be an interesting read and should be read by any developer supporting a high traffic website.
PHP – Wrap Implode Array Elements in Quotes
7Implode in PHP is a convenience function which will convert an array into a string of array elements seperated by $glue. Yesterday I needed a way to easily wrap these elements within quotes (to echo out for a javascript array) and didn’t want to write a foreach loop to do what implode does already.
To wrap the array elements in quotes, or anything for that matter, use the following:
$myArray = array('A', 'B', 'C');
echo "'" . implode("','", $myArray) . "'"; //Displays 'A', 'B', 'C'
echo implode(',', $myArray); //Displays A, B, C
News – Facebook’s ‘HipHop PHP’ Available as Open Source
1Have no idea why they code-named it ‘hip hop’ but if the article is correct, it could mean a huge boon for the PHP community. Less resource usage equals less servers equals more money available for R&D into new web technologies.
+100 to Facebook development team for not only optimizing a widely used language, but making it available as open source. Grats guys on this exciting result and look forward to working with it.
Full article @ http://gigaom.com/2010/02/02/with-hiphop-facebook-gives-php-a-turbo-charge/
News – Facebook Developer Attempting to Re-write PHP as a Compiled Lanuage
0According to an interview conducted by Phil Wong from ‘Rumpus’ (http://therumpus.net) of an anonymous Facebook (http://facebook.com) employee, there is a Facebook developer attempting to re-write PHP as a compiled language. The resulting language would be called Hyper-PHP, or HPHP, for short. A daunting undertaking to say the least.
There is no mention if HPHP would be made available as an open-source project, but I suspect that it wouldn’t be, especially if Facebook is fronting the money to develop it. According to Phil, the gains they would get from HPHP vs PHP would be in the form of 80% reduction of cpu usage which theoretically will result in faster page renders.
You can read the article in it’s entirety here: http://therumpus.net/2010/01/conversations-about-the-internet-5-anonymous-facebook-employee/?full=yes . The discussion about HPHP is about half-way down the page (just search for HPHP and you be taken right to it).
Wonder if they realize that the fully spelled out acronym of HPHP is going to be Hyper Hypertext Preprocessor? Guess it beats Super Ultra Deluxe Hypertext Preprocessor.
PHP – PDO – Prepared Statements
3If you are not familiar with PDO, please read http://melikedev.com/2009/12/28/php-pdo-basic-introduction/ first, as this article assumes basic working knowledge of PDO.
Prepared statements are supported by MySQL and provide a number of benefits in using them. A few benefits are:
- Standardized escaping of potentially unsafe characters such as double quote, single quote, and slash.
- Optimizes queries by only having to parse a query one time and “hold it” for loop iteration.
- Optimizes network traffic and server overhead
Read this great article on MySQL prepared statements for more info; http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
Now that we covered MySQL prepared statements lets move onto PDO’s implementation of prepared statements. As you know PDO is an API that standardizes calls to prepared constructs regardless of the DB server being queried. Consider the following code:
$sql = "SELECT * FROM someTable WHERE user_id = :userId";
$statement = $dbo->prepare($sql);
$statement->bindValue(':userId', $userId);
Regardless of the DB server being queried, the call to prepare a query is exactly the same. And as mentioned earlier, if $userId had any double quotes, single quotes, or slashes, they would be properly escaped.
You may have also noticed the ‘:userId’ in the bindValue() call. This is how you hook into your query with a specific variable.
Consider the following code example, this demonstrates the power of a prepared statement with respect to multiple records, and touches on the optimization discussed earlier:
$sql = "INSERT INTO users SET first_name = :firstName, SET last_name = : lastName, SET ssn = :ssn";
$stmt = $dbo->prepare($sql);
foreach ($usersToInsert as $user) {
$stmt->bindValue(':firstName', $user->getFirstName());
$stmt->bindValue(':lastName', $user->getLastName());
$stmt->bindValue(':ssn', $user->getSsn());
$stmt->execute();
}
In the above example, your DB server only has to parse the query once even if you insert 17 million rows.
As you can see, there are several benefits to using prepared statements, and if using PHP, how PDO can be called to construct prepared statements.
PHP – PDO – Basic Introduction
1PDO is an acronym for PHP Data Objects. In a nutshell, this simply means that PDO acts as a DAL (data-access abstraction layer), which provides an API (application programming interface) used for interacting with a DB server. PDO is only used with PHP (hence the PHP part of the acronym) and offers the following benefits (just to name a few):
- Access various DB servers (MySQL, Oracle, Postgres) without having to change any calling code
- Access to prepared statements. Useful for escaping query level variables among other things
- Access to other various convenience methods, ensuring standardized calls.
Check out http://php.net/pdo for more info.
This article is short and sweet, but will be the foundation for other articles.
PHP get_class_methods (useful dev tool)
1So you have an object, and you have no idea what methods you have available to you? No prob… just copy and paste the following line and wham, instant view of all the methods available to your object. I find this extremely useful when working in symfony where some stuff is so abstracted you’re not sure what you are dealing with anymore ($this->object).
foreach (get_class_methods($object) as $method) { echo 'M: ' . $method . '<br>'; } exit;
