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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:05:50+00:00 2026-06-14T17:05:50+00:00

Today at work I got stucked in a line of code I can’t achieve

  • 0

Today at work I got stucked in a line of code I can’t achieve to understand.
I had a function in JavaScript that, to ensure getting an unique timestamp did that ugly loop:

var ts = +new Date();
while (ts === (ts = +new Date()));

I think it’s clear to understand that we’re re-setting ts until it’s value becomes different.

But when I tried to migrate the function to PHP code – like that:

$ts = preg_replace('/^\d\.(\d+)\s(\d+)$/', '$2$1', microtime());
//regex extracts digits and re-positions them - ugly microtime :(
while ($ts === ($ts = preg_replace('/^\d\.(\d+)\s(\d+)$/', '$2$1', microtime())));

interpreter enters an infinite loop. All my tries seemed to point that $ts assignment is taking place before comparison, but I can’t understand why left-side expression isn’t being resolved first. My PHP knowns are not as big as my JavaScript ones, so I’m wondering if there is some key tip about PHP expressions and statements I’m leaking.

Advice: I don’t want to fix my code by making a “well constructed” loop – the solution is obvious – I’d love to understand what’s happening here. All help is much appreciated.

  • 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-06-14T17:05:50+00:00Added an answer on June 14, 2026 at 5:05 pm

    PHP needs the assignment to be evaluated first, before a comparison can be made. Normally, the assignment operator would have a very low precedence, so when you write $a = $b + $c, the addition is calculated first, before the result is assigned to $a.
    However, it is a special case, and although it doesn’t seem to be documented very well, a similar case is found in the documentation, stating:

    Although = has a lower precedence than most other operators, PHP will
    still allow expressions similar to the following: if (!$a = foo()), in
    which case the return value of foo() is put into $a.

    So in this case, PHP determines that it needs to do the assignment before it can negate the result of the assignment. I think a similar thing is happening here, although you’re right that PHP should be able to do the assignment later.

    Actually, the result might be unpredictable, as is the case with some other expressions:

    // mixing ++ and + produces undefined behavior
    $a = 1;
    echo ++$a + $a++; // may print 4 or 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across some javascript at work today that used jQuery to fetch two
Today I got our svn repository cloned into git. I had some uncommitted work
I have some code that used to work. But I today when I try
I ran into an interesting problem at work today. I got a request to
Today at work someone tried to convince me that: {$obj->getTableInfo()} is fine for smarty/mvc/templating
I work in an asp.net shop, and I heard today that the bottleneck on
So I got my code to work just fine in Chrome. In FF I
Today I got a EXC_BAD_ACCESS ,so I debug the code to solve the problem.
I haven't got problems with code since my last problem with interface covariance. Today
I pulled the latest code using git today and I got the following error:

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.