473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Foreach, If and Variables - Weird variable evaluation

Hi,

I am having a rather frustrating time with a script I am working on.

I am looping through the elements of an array with a foreach statment
and inside the foreach statement I have two if statements.

An an example:

foreach ($a as $b) {
if ($b == $value1) {
statement1;
}
if ($b == $value2) {
statement2;
}
}

If I replace $value1 and $value2 with actual values, both if statements
are tested. However is I leave things as they are using varibles
containing the values I want to check for only the first if statement
is tested. It is acting as though it is breaking out of the foreach
when the first if statement evaluates to true. This does not happen
with actual values in place of $value1 and $value2 variables.

If anyone can shed light on this problem, the help is greatly
appreciated.

TIA,
Adam

Jul 17 '05 #1
1 5430
*** Adam wrote/escribió (27 Jun 2005 21:13:04 -0700):
foreach ($a as $b) {
if ($b == $value1) {
statement1;
}
if ($b == $value2) {
statement2;
}
}

If I replace $value1 and $value2 with actual values, both if statements
are tested. However is I leave things as they are using varibles
containing the values I want to check for only the first if statement
is tested. It is acting as though it is breaking out of the foreach
when the first if statement evaluates to true. This does not happen
with actual values in place of $value1 and $value2 variables.


Your pseudo-code looks correct, so the problem must in the actual code you
don't paste.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Geoff | last post by:
Here's the situation. I have a static html page which we want to update to include some dynamic content. I want a counter that keeps track of the number of times anyone presses the "add" button,...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
30
by: Javaman59 | last post by:
I come from a background of Ada and C++ programming, where it was considered good practice to explicitly initialize every variable. Now that I'm programming in C# I think that it would be best...
3
by: Primera | last post by:
I am trying to use the below to return different values of an int variable, but I seem to be running into scope issues as I cannot use the iSize variable outside of the foreach block. Any help is...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
4
by: bg_ie | last post by:
Hi, What is wrong with the following - short arrfile = new short; foreach (short s in arrfile) { s = 10; }
9
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.