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 6584461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:32:23+00:00 2026-05-25T16:32:23+00:00

When an expression is a function that returns some kind of value, is there

  • 0

When an expression is a function that returns some kind of value, is there a way to get it’s processed value in a temporary variable, that can be accessed only within matching expression?

Let’s make a random example.

function used in expressions:

function crazy($input){
    return $input * 5 - mt_rand(0, 5);
}

And how I see this should work:

if(crazy(2) > 5){
    $result = $crazyTemporaryVariable . ' crazies'; 
    // because calling crazy(2) here again will:
    // 1) process it again, 
    // 2) because of the `mt_rand();` return different value.
}else{
    $result = 'nothing can happen here, because $crazyTemporaryVariable is not set';
}

In other words, it should create a temporary variable if the expression is true, else, "nothing" is left behind.

I’m aware that this can be done with the function call before the expression and assigning it’s returned value to a previously set variable. But, you have to create a new variable- I see a small, but still a performance drop, because if the expression doesn’t meet the requirements- the variable is useless, but stays defined.

// we have our crazy function above, a new expression

$crazy = crazy(2);
if($crazy > 5){
    $result = $crazy . ' crazies';
}else{
    $result = 'anything...';
}

// or...

$result = $crazy > 5 ? $crazy . ' crazies' : 'anything';

A note, this is PHP environment where I haven’t seen this behavior/possibility nor in any other language which I’ve come in contact with (there are not much tho’).

One more thing, it nicely applies to loops too:

while(crazy(2) <= 5){
    $result = $crazyTemporaryVariable . ' crazies';
}

// where normally you'd have to change the variable value;
$crazy = crazy(2);
while($crazy <= 5){
    $result = $crazy . ' crazies';
    $crazy = crazy(2);
}

Note: $crazyTemporaryVariable is just an imaginary variable. And if I’ve overlooked it, I doubt that it’s a variable, think it could be a processing time constant EXPRESSION_RESULT, or something along those lines. But because of my English limitations, I have no idea what to look for to find it.

So yes, the key here is- defined, therefore accessible, only if expression is true.

So the question is, are there languages that support this and if I’ve overlooked it in PHP how can I access it then + reference, please?

After writing all what’s above, I rememebered about this behavior:

if(($crazy = crazy(2)) > 5){
    $result = $crazy . ' crazies';
}else{
    $result = $crazy . ' crazies, but... not crazy enough.';
}

This is almost what I’m looking for, but it’s nearly the same as defining our variable before, and you can access the result of the function if the expression matches, therefore, it won’t cut this time.

Thanks in advance!

imaginary example

Ternary and javascript..

// could be: (keep in mind, this would not work with our _crazy_ function, because of random number.)
var result = string && string.search(/\s/) ? string.substr(0, __tempvar__) : string; // not available
// ...but instead is
var result = string.search(/\s/g) > 0 ? string.substring(0, string.search(/\s/g)) : string; // +1 repeated function call that affects performance.
  • 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-25T16:32:24+00:00Added an answer on May 25, 2026 at 4:32 pm

    I see a small, but still a performance drop, because if the expression doesn’t meet the requirements- the variable is useless, but stays defined.

    Whether declaring a variable makes any performance difference at all depends on the language and its implementation. Whether the performance difference matters depends on the application; probably only if the variable refers to a very large object that is not destroyed/GC’d because it still has a reference. Variables themselves should be practically free in any half-decent language implementation.

    In other words, it should create a temporary variable if the expression is true, else, “nothing” is left behind.

    This makes your language’s scoping rules very complicated. What you might want to do in a language with C-like scoping rules is:

    # declare $result
    {
        $crazy = crazy(2);
        if ($crazy > 5){
            $result = $crazy . ' crazies';
        } else {
            $result = 'anything...';
        }
        # end of scope for $crazy, so it disappears
        # and the object it refers to takes up no more space
    }
    

    So the question is, are there languages that support this

    IIRC, Perl stores the value of the last expression (or something like that) in $_. I personally find that hideous. In short, you really shouldn’t want this; there are simpler, more principled ways of dealing with this problem.

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

Sidebar

Related Questions

I'm trying to write a regular expression (via Autohotkey's RegExReplace function) that will enforce
I'm trying to make a Javascript function that will take a mathematical expression and
What would the regex expression that would go into preg_split function to validate date
I am trying to write a function that returns T/F in regards to if
In some languages, you can do $a = $b OR $c OR die(no value);
Why do some languages differentiate between methods that return a value and methods that
Say that have the following CTE that returns the level of some tree data
I am trying to get some xpath from xsl variable using xsl ver 1.0
I have a custom type MyType with a function MyBoolFunction(string) that returns true or
I am using stopifnot and I understand it just returns the first value that

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.