PHP

Unexpected results of integer to string comparisons

If you want to make a php programmer applicant cry, give them this bit of code and ask them to predict the output.

Ignore section of code styling standards.

If you have a section of code that is not passing code style review and due to its nature, never can and you have made the determination that it is safe to ignore it:

White screen in Drupal Development

In Drupal development it can be annoying to have just a blank white screen of death WSOD present you with no helpful information.

Adding these lines to your settings.php file will help make the WSOD more helpful
REMOVE them from production server.

LAMP using XAMPP

XAMMP can be a quick and easy setup of apache, php and mysql.
http://www.apachefriends.org/

The working directory for the install is

/opt/lamp/htdocs

Apache Conf File (httpd.conf): /opt/lammp/apache/conf/httpd.conf

PHP Conf File (php.ini): /opt/lammp/xampp/apache/bin/php.ini

MySQL Conf File (my.cnf): /opt/lammp/xampp/mysql/bin/my.cnf

php output debug

This output debug is is a quick snippet to use when Devel's krumo output is not available.

Template Tracers

This code for template tracers should be used with caution. They do expose where files live (less secure).
Opening tracer:

<!-- <template> <?=str_replace('var/www/',  '' , __FILE__  );?>  -->

Closing tracer:

<!-- </template> <?=str_replace('var/www/',  '' , __FILE__  );?>  -->

Live server / Development server switch

Sometimes you need different values to be used on dev and live servers.

Here are two examples:

LAMP setup on xubuntu

Quick code for installing the LAMP stack on xubuntu:

#Install SSH
sudo apt-get install openssh-server

#Install Apache
sudo apt-get install apache2
# apache should now return a response  at http://127.0.1.1/

#likely not needed but for some reason a lot of sites reference it
sudo apt-get install libapache2-mod-auth-mysql

#Install MySQL
sudo apt-get install mysql-server

PHP Variable safety - to avoid Notices

Simple IF statements are often used, especially in Drupal Theme-ing. ;They look like this

Safe PHP foreach

A foreach thows non-fatal warnings if you pass it a non-existent array or a string or integer. It does not mind, if you toss it an empty array. So I use the following ternary logic within the declaration of the foreach. If $aResults is an array OR an object, then it uses that in the foreach ELSE pass it an empty array. This makes it fail without throwing any errors.

Pages

Subscribe to RSS - PHP