Syntactic Sugar in PHP

Asked

Viewed 176 times

3

The foreach would be an implementation of Syntactic Sugar in PHP?

foreach($array as $key=>$values) {
    //Faz algo com as chaves e valores.
}
  • 2

    Based on another high-level language like C#, I believe it is Syntatic Sugar because there is no foreachin C then either he’s based on a whileor in a for. I wanted to be sure, but I can’t find any information that is useful in any repository of something written in C >.<

  • 1

    @Brunoaugusto It is possible to make a kind of foreach in C and C++, in the same way as jQuery’s people did a kind of "foreach function" (the function each jQuery). Macros also help. But in C this is more laborious, because unlike in C++, in C there is no concept of reference.

  • I don’t know C, I can even read something in C, but that wouldn’t be, I don’t know, something like a function user-defined that we have in high-level languages?

  • 1

    It is only syntactic sugar, if it is implemented by the language itself, built-in. If it is implemented by the user it is no longer, to my understanding.

  • I know not, Marcelo. Once a forum user gave me an ear tug because I had added an empty abstract class only to not implement the same interface in several classes manually.

  • Sorry I didn’t understand.

Show 1 more comment

3 answers

4


Yes, if since it would not need the same to achieve such result, as well as many other functions: while and do while.

But, the languages must evolve and this ends up being considered natural over time (after all nobody wants to keep writing repetitions for with if and GOTO).

3

Virtually all repeating structures (for, foreach, do-while, for-in) of the most modern programming languages can be considered syntactic sugar for the simplest structure of all, the while (which is present in PHP since version 4). So the answer is yes.

It is worth remembering that syntactic sugar are language structures that allow to perform a task in a more "simple" way than by the more "low level" form. For example, if we compare for and while in PHP:

while:

$i = 0;
while ($i <= 10):
    echo $i;
    $i++;
endwhile;

for:

for ($i = 0; $i <= 10; $i++) {
    echo $i;
}

Most people consider the second most simple and readable form. However, in terms of computation, both forms are equivalent.

2

According to the manual, yes and no.

For example, they are identical:

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
?>

and

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>

however, they are no longer in that case:

<?php 
$arr = array(1,2,3,4,5,6,7,8,9); 

foreach($arr as $key=>$value) 
{ 
    unset($arr[$key + 1]); 
    echo $value . PHP_EOL; 
} 
?> 

Output: 1 2 3 4 5 6 7 8 9

and in that:

<?php 
$arr = array(1,2,3,4,5,6,7,8,9); 

while (list($key, $value) = each($arr)) 
{ 
    unset($arr[$key + 1]); 
    echo $value . PHP_EOL; 
} 
?> 

Output: 1 3 5 7 9

  • That doesn’t mean it’s not syntactic sugar. The output is not the same in the last two examples because the logic that the program will follow is not the same - by using unset, you are modifying the array, but the internal pointer will still access the values that should be removed. Regarding the use of unset within an iteration of foreach, the manual itself says the following: As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior.

  • I agree, but it’s good to make it clear that not every use will come in the same.

  • What he meant is that it would be possible to do similar things with these functions, the way he implements can change, but it can come to the same result.

Browser other questions tagged

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