7 Replies - 280 Views - Last Post: 29 July 2012 - 04:19 AM Rate Topic: -----

#1 fgatlin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-July 12

arrays will not display as needed

Posted 23 July 2012 - 08:48 AM

Hi everyone,

I am quite a newbie at PHP, but I am trying to write a piece of code that will do something IF the zip-code is true,
ELSE do something else if false.

However, I have a list of about 50 local zip-codes to use.

if($stZip == 33762){
$locDeliv = $inpOpts['loc_ch_av'];
}else {
$locDeliv = $inpOpts['loc_unch_av'];
}


Works well, but I need to include multiple zip-codes.

$zips[]= "33762";
$zips[]= "33716";
$zips[]= "33634";
if($stZip == $zips){
$locDeliv = $inpOpts['loc_ch_av'];
}else {
$locDeliv = $inpOpts['loc_unch_av'];
}


The above code does NOT work, and only displays the variable $zips (not an actual zip-code)

Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: arrays will not display as needed

#2 Slice  Icon User is offline

  • D.I.C Addict


Reputation: 197
  • View blog
  • Posts: 595
  • Joined: 24-November 08

Re: arrays will not display as needed

Posted 23 July 2012 - 09:21 AM

So you have one zip code ($stZip) and you want to check through an array of multiple zips to see if any match it? Try a foreach loop like this:

<?php
$zips = array();
$zips[]= "33762";
$zips[]= "33716";
$zips[]= "33634";

foreach($zips as $zip)
{
	if($stZip == $zip)
	{
		$zipmatch = true;
	}
}

if($zipmatch){
	$locDeliv = $inpOpts['loc_ch_av'];
}else {
	$locDeliv = $inpOpts['loc_unch_av'];
}
?>



If there are no zip codes in the array that match $stZip then it will return false.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,525
  • Joined: 23-August 08

Re: arrays will not display as needed

Posted 23 July 2012 - 09:41 AM

Or

<?php

$zips[]= "33762";
$zips[]= "33716";
$zips[]= "33634";
if (in_array("33716", $zips)) {
	print("Found it\n");
} else {
	print("Not found\n");
}

?>

Was This Post Helpful? 3
  • +
  • -

#4 fgatlin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-July 12

Re: arrays will not display as needed

Posted 23 July 2012 - 10:01 AM

Worked PERFECT Slice! Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 Loopzle  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 35
  • Joined: 23-October 10

Re: arrays will not display as needed

Posted 29 July 2012 - 02:27 AM

Although this may not help for the current topic (which has been solved), just a note for next time:
if($stZip == 33762){
$locDeliv = $inpOpts['loc_ch_av'];
}else {
$locDeliv = $inpOpts['loc_unch_av'];
}


can be shortened to
$locDeliv = ($stZip == 33762) ? $inpOpts['loc_ch_av'] : $inpOpts['loc_unch_av'];



:)
Was This Post Helpful? 0
  • +
  • -

#6 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 162
  • View blog
  • Posts: 588
  • Joined: 12-October 09

Re: arrays will not display as needed

Posted 29 July 2012 - 03:21 AM

Let's not try to get him running before he can walk..
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: arrays will not display as needed

Posted 29 July 2012 - 03:28 AM

@Loopzle. A huge downside to that, however, is that ternary conditions tend to be a lot more difficult to read. Sure, you save 20 characters, but in return you have to bunch the whole thing up in a single line, forcing you to have to scan the line for the ? and : characters in order to separate the condition and the return values. - It's even worse if you try to spread it over multiple lines, or if there are multiple conditions. Or the horror of using multiple, nested ternary operators.

Reducing the number of lines of code means far less than improving readability. Not that using the ternary operator always means reduced readability, but in my experience, an overwhelming majority of the times it does.
// This is fine.
$value = $cond == 'value' ? $iftrue : $otherwise;

// This is bearable, I suppose.
$value = isset($_GET['inputParam']) ? $_GET['inputParam'] : $defaultValue;

// Now it's getting a bit out of hand.
$user = User::Validate($_POST['username'], $_POST['password']) ? new User($_POST['username']) : null;

// And this is just plain ridiculous.
$ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);


Was This Post Helpful? 2
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,531
  • Joined: 08-August 08

Re: arrays will not display as needed

Posted 29 July 2012 - 04:19 AM

If we're going to make things readable (and we certainly should) then let's not forget about proper indenting:
if($stZip == 33762){
	$locDeliv = $inpOpts['loc_ch_av'];
}else {
	$locDeliv = $inpOpts['loc_unch_av'];
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1