[UPHPU] in_array doesn't typecast?

Lonnie Olson lists at kittypee.com
Tue Nov 3 14:29:10 MST 2009


On Tue, Nov 3, 2009 at 9:26 AM, Jordan Schatz <white.armor at gmail.com> wrote:
> Anyone else have this experience? I generally don't have to typecast
> before a comparison, but in_array seems to require it?

I like really simple code.

<?php
//Test Data
$primary_phone = '18017848218';

// Load the zip code information
$area_codes = file('./area_codes',FILE_IGNORE_NEW_LINES);

$area_code = substr($primary_phone, 1, 3);
//Doesnt Work
if (!in_array($area_code, $area_codes)) {
 $error_message = 'PLEASE ENTER A VALID PHONE NUMBER!';
 $commit = 0;
 }

Reading in the file using file()
http://www.php.net/manual/en/function.file.php will automatically put
the lines into an array, but also the flag passed will auto-chop the
newlines at the end, which is what is causing your type casting
problems.

--lonnie


More information about the UPHPU mailing list