![]() So an $a++ results into 2 opcodes ( POST_INC and FREE), $a += 1 into one ( ASSIGN_ADD), and $a = $a + 1 into Line #* E I O op fetch ext return operands Our example code will output the following byte code: ![]() Gets interpreted, but not the actual PHP source code). When a PHP script runs, the first thing it does is actually compile your PHP code into an intermediate format calledīyte code (this also debunks the fact that PHP is a truly interpreted language, it’s the byte code that But let’s take a look what is actually happening under the hood of PHP when executing the code. Result in different characters, and guessed the fop string right, but the two int(1)’s? Where do they comeįrom? From a PHP developer’s point of view, it seems very inconsistent and now it seems that these three statementsĪctually aren’t equal. I reckon most aren’t expecting this outcome! Maybe some of you probably knew that adding something to a string will But let’s take a look at another example: $a = "foo" $a ++ var_dump ( $a ) $a = "foo" $a += 1 var_dump ( $a ) $a = "foo" $a = $a + 1 var_dump ( $a ) string ( 3 ) "fop" int ( 1 ) int ( 1 ) So it seems that using $a++ is just as valid as using $a += 1įor incrementing. Seems intuitive enough and they look all equal enough. But will they all result in the same output? int ( 2 ) int ( 2 ) int ( 2 ) ![]() Take a look at these three examples: $a = 1 $a ++ # Unary increment operator var_dump ( $a ) $b = 1 $b += 1 # Add assignment operator var_dump ( $b ) $c = 1 $c = $c + 1 # Standard add operator var_dump ( $c ) ĭifferent code, but all three blocks will increment the number. There seems to be many different ways of adding 1 to a variable. Similar, they work and behave differently under the hood of PHP, which can lead to - let’s say - interesting results. So, there are a few different ways to increment a value, and they MIGHT seem View that might seem the case, but is it really? There are bound to be some catches to it (otherwise we wouldn’t write aīlogpost about it). That sounds like a simple enough job right? Well. « Understanding Symfony2 Forms Symfony, XDebug and the maximum nesting level »
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |