Conditional Assignment in PHP

Asked

Viewed 80 times

3

I’m studying PHP and came across a Feature which does not exist in Python (I do not know in other languages), which is the assignment in a conditional test:

$file = fopen("arquivo.txt", "w");
while($row = fgets($file)){
    ...
}

Correct me if I’m wrong but in a conditional test, the same search check the result of the condition or the state of the object for example an empty list is false, what we call Truthy and Falsey in Javascript. My doubts are:

  1. That one Feature exists only in PHP?
  2. How does it work underneath the covers?

1 answer

5


This Feature exists only in PHP?

No, it exists in virtually all languages, and even in Python with some restriction (at least in current version)

How does it work underneath the covers?

There’s nothing else, this is just a syntactic matter, just let the compiler write it like this, the execution will be the same as the form no inline.

In Assembly each instruction (very basic) goes in a row, high-level languages permutes put several of them in the same line, how much this is allowed is decision of each language. Under the same curtain this involves a huge "collapse", but without wanting to do this in the background we can understand this (note that the while is an abstraction):

inicio:
    $row = fgets($file);
    if (!$row) goto fim;
    ...
    goto inicio;
fim:

I put in the Github for future reference.

  • Interesting Maniero. That would theoretically be what happens "under the cloths" in a while? type.. (analysis of the condition at that moment) ... If it’s true go back to the beginning, if not, go to the end... Excuse my ignorance. =)

  • Yeah, that’s right, that’s right

  • I have a lot to study... XD

  • 2

    But you have curiosity and this helps a lot, if you need to ask more useful questions like this.

  • @Andreicoelho No, actually it is a slower order of magnitude, interpret the results right. In fact the results are inconsistent and the slowest was the goto, probably because the other ways make optimizations.

  • I got Maniero. Thank you. I will check better. Anyway thanks for the encouragement and attention.

  • True! I traveled in code! kkkkk Thanks!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.