jimfog Posted October 31, 2013 Share Posted October 31, 2013 I have an expression like this: if(($passwcheck['oldpass']==='')&&($passwcheck['newpass']==='')&&($passwcheck['newretyped']==='')) The code inside the brackets must be run ONLY if all three of the conditions are met. I am trying to use a foreach loop instead: foreach ($passwcheck as $value) { if ($value=='') { //code to run } } The above though, does not do my job...even if $value is =='' one time the code inside the brackets will run. The aim(I repeat it) is that all 3 $value must be equal to "" in order for the code to run. Can the above be done with the foreach loop or I should opt for the conditionals? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 31, 2013 Share Posted October 31, 2013 The only reason to do something like this is if you don't know how many values you are testing: $flag = true;foreach ($ar as $val) { if ($val !== '') { $flag = false; }} if ($flag) { // whatever you want to do} You could also use the array_filter function with a callback to filter empty strings out of the array and then check to see if there are any elements left in the array. Link to comment Share on other sites More sharing options...
jimfog Posted October 31, 2013 Author Share Posted October 31, 2013 I will use array_filter instead...but as you have already said research must be done. I went to the manual for the above function and I really cannot understand the first example#1. Let us take for example the even function: function even($var){ // returns whether the input integer is even return(!($var & 1));} How does the above code examines if the supplied array member is even? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 31, 2013 Share Posted October 31, 2013 It uses the bitwise and operator to determine if the last bit in the number is a 1 or 0 and then negates the result. How the callback function determines whether an array value is "valid" isn't really important to the example though, the point is that you can use a function to test each array member for yourself. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now