Posts tagged with PHP

Typecasting Strings to Integers in PHP

August 15th, 2008

PHP does some funny things when typecasting strings as integers and may not work the way you would expect (to be fair, it is documented). Here are some examples running under PHP 5.2.5 from the Mac OS X command line:

php -r 'var_dump((int) "agbae");'
int(0)
php -r 'var_dump((int) "zzzzzzzzzzzzzzzzzzzzzzz");'
int(0)
php -r 'var_dump((int) "2agbae");'
int(2)
php -r 'var_dump((float) "42165.6agbae");'
float(42165.6)

This is important to know because PHP allows you refer characters of strings just like you would the elements of an array (and somewhat similar to C’s strings):

php -r '$s = "abcdef"; var_dump($s[2]);'
string(1) "c"

However, giving an invalid key won’t given an error, depending on how your installation of PHP is configured in php.ini. If you give an out-of-bounds string you get an empty string:

php -r '$s = "abcdef"; var_dump($s[45]);'
PHP Notice:  Uninitialized string offset:  45 in Command line code on line 1
string(0) ""

If you give a string key, it is always for the zeroth index, since it is typecasted to an int and typecasting works as described above:

php -r '$s = "abcdef"; var_dump($s['zzzzz']);'
PHP Notice:  Use of undefined constant zzzzz - assumed 'zzzzz' in Command line code on line 1
string(1) "a"

From the Laboratory: Image Resizing and Thumbnail Creation

February 24th, 2008

This is the first in a series of posts on technical issues related to web site development and making user-friendly websites. These will focus on the technical details of developing web sites and applications so will probably be interesting to only a subset of readers. Within the next few months, time permitting, we will be launching a new section of the site called Bubble Foundry Labs, where you will be able to find both technical articles and experimental web applications. For non-technical types, these demo applications may prove easier to understand than this article series.

Read more »