Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6816327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:54:44+00:00 2026-05-26T20:54:44+00:00

My goal is to put some complex logic in an if() statement. Let’s say

  • 0

My goal is to put some complex logic in an if() statement. Let’s say I have an array of values and I’m going to execute a bit of code if everything in my array is nonzero. Normally, I could say, $valid = true, foreach my array, and set $valid = false when a zero is found. Then I’d run my code if ($valid). Alternatively, I could put my loop into a function and put the function intop my if().

But I’m lazy, so I’d rather not muck about with a bunch of “valid” flags, and I’d rather not write a whole new function that’s only being used in one place.

So let’s say I have this:

if ($q = function() { return 'foo'; }) {
  echo $q;
}
else {
  echo 'false';
}

I was expecting that the if gets 'foo', which evaluates to true. My closure is committed to $q and the statement executes. $q returns string foo, and ‘foo’ is printed.

Instead, I get the error Object of class Closure could not be converted to string.

So let’s try this instead:

if (function() { return false; }) {
  echo 'foo';
}
else {
  echo 'true';
}

I was expecting that my function would return false and ‘true’ would be printed. Instead, ‘foo’ is printed.

What is wrong about the way that I’m going about this? It seems like it’s saying, “Yep, that sure is a function!” instead of “No, because the function evaluated to false.” Is there a way to do what I’m trying to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:54:45+00:00Added an answer on May 26, 2026 at 8:54 pm

    function() { return false; } creates an object of type Closure, similar to new with other class-types, compare the following code:

    $func = function() { return false; };
    

    $func now is an object. Each object returns true in an if clause. So

    if ($func)
    {
      # true
    } else {
      # will never go here.
    }
    

    You might want to do this instead:

    if ($func())
    {
      # true
    } else {
      # false
    }
    

    which will invoke the Closure object $func and give it’s return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

goal: I have the string 1234432144 I want to only replace the first 2
My Goal I would like to have a main processing thread (non GUI), and
my goal is to write a stored proc that can collect all field values
I am trying to create a loop statement for some of my code and
I've been doing history tables for some time now in databases, but never put
I have some buttons on a tabwidget. These buttons need to have some icon
Here's my goal : I have a Windows XP PC with all the source
I have been using Java for quite some time, yet my education in loops
I'm new to LINQ and am finding it a bit difficult with understanding some
I have written some jQuery to get a value and then store it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.