PHP equivalent to Perl's unless conditional

Could someone give me an example of PHP’s equivalent to Perl’s unless conditional statement?

Perl example



unless ($this or $that) {
    print 'other'; }
 

if either of the two specific conditions exist (this or that) I don’t want it to print other, make sense?


if (!$this AND !$that)
{
  dothisstuff();
}

If this exists, that exists, or both exists, do NOT output?

if (isset($this) || isset($that))
{
    echo 'other';
}

Technically speaking I’m pretty sure you can go “if ($this or $that)” too. OR and || have similar functions in PHP (especally in if statements), so you can use the OR method if you’d like, and I’m pretty sure those variables would be considered “false” in the sense that the if statement would fail.

I don’t want AND I want OR, is this logical…


if (!$this || !$that)
{
  dothisstuff();
} 

thanks someone, but isset is checking if it is true, correct? I want it to be false in order to print.

I think you do want AND, unless I’m missing something.

You want it to print “other” if both conditions are FALSE, right?

If condition 1 is false and condition 2 is false, print other.

No I want, If condition 1 is false or condition 2 is false, print other. Both condtions may not exist at the same time but if either does, don’t print.

1=true, 2=false: Print.
1=false, 2=true: Print.
1=true, 2=true: Don’t print
1=false, 2=false: Don’t print

That’s your logic?
How about just if (isset($one) != isset($two))?

lol this is fun… no

my logic

1=true, 2=false: don’t print.
1=false, 2=true: don’t print.
1=true, 2=true: don’t print
1=false, 2=false: print


if(!$this XOR !$that){
dostuff();
} else {
//Either one variable is true or both variables are true or both variables are false
}

If either value is not true but NOT if both values are not true.

Just for those who don’t know, here’s the PHP operator precedence info :slight_smile:

Right.

If condition 1 is false AND condition 2 is false, print other.


if (!$this AND !$that)
{
    print "other";
}

So why were you saying “or”? If one AND the other, that’s an and. :wink:

if (!isset($one) && !isset($two))
{
   echo 'other';
}

For future reference, if you ever need help with a boolean expression, your best bet is probably to list off all of the options/possibilities like you just did.

isset() might not be the best choice, because it only determines if a variable is set. Once again…php.net words it better than me, so here ya go:

isset doesn’t reliably evaluate variables with blank strings (not necessarily NULL).
i.e.
$blankvar = “”; // isset will return true on this.

This is a very common pitfall when handling HTML forms that return blank text fields to the script. You’re better off doing this:

if ($var != “”)
return true;
else
return false;

This more of a programming practice rather than the function’s shortcomings. So if you have a habit of initializing variables you’re likely to run into problems with isset() if your code or php project become very large.

Hence:


$foo = false;
if(isset($foo)){
	echo "TRUE";
}

Will echo TRUE.

And:


$foo = true;
if(!isset($foo)){
	echo "TRUE";
} else {
	echo "FALSE";
}

Will echo FALSE.

So it depends if he wants to check if a var is set, NULL, or false. Subtle differences :slight_smile:

Yeah, I know… but if you read his first put, he talks about existance.

But then later on he used true/false.

/exits thread…too confusing :stuck_out_tongue:

Yeah yeah yeah. :stuck_out_tongue:

I’m use to saying OR…with Perl’s unless condition you’re checking if something is false rather than true. So I can list as many OR’s I like in the condition. If I used AND both would have to be present for it be false.

Let’s say I have a member id variable ($member_id). The value of $member_id would change depending on which member it was, they all have unique id’s. Now I want to track each member’s movement execpt for two specific member’s. With Perl’s unless statement I can simply do this…


unless ($member_id == '100' or $member_id == '101') {
   print 'I'm tracking member's movements except for Member 100 and 101';
}

PHP should adopt the unless conditional. With unless the attached statements are only executed if the condition is false. Seems simpler to me.

btw, thanks everyone for the help. I did figure it out, it involved using AND

Oh. That’s interesting. The PHP equivilent of that would be if ($member_id != 100 && $member_id != 101).

Why? unless() is longer than if, and a simple ! (not) is nice and easy. There ain’t much difference.

The only confusing thing here is that you couldn’t word the question precisely :slight_smile: