General Question

Zyx's avatar

PHP problem with && and possibly a malfuntioning array?

Asked by Zyx (4160points) October 29th, 2010

I have been working on this page for over a year now and I’m pretty sick of it but it was all going so well when I started…

I noticed I didn’t wrap my statements in () those things when using && but then again they do all get checked == against the same number (just 1, it’s a bunch of switches in an array)

if( $a[1]=1 && $a[2]==1 && $a[3]==1 ) { }
simplified but pretty much the deal

I was supposed to have four images and ony one showed up, and it’s in the wrong place. I also created a second array but left the first couple of values undefined.

for example

This project is massively… well, massive. So it would just be cruel to throw anyone else into it. But if any of the things I’ve mentioned might cause problems I’d like to know.

Observing members: 0 Composing members: 0

3 Answers

funkdaddy's avatar

Unfortunately the examples above don’t really shed a lot of light without some additional information. Maybe we can help with some troubleshooting tips in general? A couple of things that come to mind…

> Have you tried making sure your array contains what you are expecting before your if statement? A great way to do that is the print_r function. Wrap it in html <pre> tags and it gives you a very readable output of your array without too much trouble.

> You may want to use the boolean true and false instead of 1 and 0 for your switches, unless that doesn’t fit logically, it can be easier to follow.

> Remember that with the the equal comparison (”==”) 0 evaluates to false, and basically any non-zero number will evaluate to true, if you need to make sure something is truly the number 0 or 1, you can use the identical comparison (”===”) to make sure… more info on comparisons

> The way you have your if statement set up, if any of your checks don’t equal 1, nothing inside your if statement will run, is that as you intended it?

> if you’re using a foreach loop, remember that the loop operates on a copy of the array, not the original. So any operations you do on each element will be performed on that copy and if your evaluating the original, they won’t be reflected

So for example
$array = array([‘something’] => 5);
foreach($array as $key => $value) {
$value = $value * 2;

echo $array[‘something’];

would show 5 instead of 10
With the number of people using PHP and the fact that arrays and the && operator were built in pretty early, it seems unlikely that there’s a problem with PHP. I’d take a closer look at your code and make sure everything is where you’re expecting it.

Good luck with it, I hope it all comes together.

lillycoyote's avatar

I don’t really know anything about this so all I can offer is my mother’s basic rule of thumb. This was something my mother said she often had to remind her student of and herself, sometimes and it was this: If you get to the end of some really complex formula or equation and you seemed to have arrived at the wrong answer it is, more often than not, not that you have done the higher level math wrong, but that somewhere along the line you added 2+2 and gotten 5. I’ve mentioned this one before but it has always seemed like good advice to me.

So, is it possible that your problems could be the result of your having made one or more simple, careless errors along the way? It kind of seconds @funkdaddy when he said: “I’d take a closer look at your code and make sure everything is where you’re expecting it.

You may have convinced yourself that you are doing something the wrong way and focusing so much on that, that you’ve overlooked the possiblility that something may be in the wrong place. Could it be just one stupid little thing that you’ve overlooked? Sometimes big headaches come from stupid little things.

Zyx's avatar

It might be worth mentioning my arrays are two-dimensional

@lillycoyote I’ve already rethought and rewritten it several times and though the errors appear as though caused by a bad equation they seem good to me. And I have calculated all of it several times already.

@funkdaddy I didn’t mean to actually imply something wrong with PHP ;)
Don’t think I really need ===, since == has always done what I want it to perfectly.
It’s basically a map with six directions, so at every location I check these “directions”
like this:

if($a[$x][$y]==1){ There’s a road here, this part already works }}}

once again, simplififed, my “for” statements are fine. The landscape is formed by the roads so I have some equations to see where these are and thus what the state of the map is. Up until this point I know it works since I’ve printed these “roads” individually as well. The landscape isn’t forming properly though all I’ve been doing is placing these “roads” in if statements to check if more images ($v) need to be placed.


where $b is a counter and $d is there to compensate for newlines. I’m still worried I should be wrapping my $a[][] before using &&...

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther