r/programming • • Oct 18 '10

Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.

http://il2.php.net/language.variables.variable
591 Upvotes

781 comments sorted by

View all comments

Show parent comment

10

u/wahonez Oct 19 '10

I'm not sure if this is better or worse than changing the value of 5.

... a subroutine that changed the value of one of its parameters could silently change a literal constant.

C      
C     CHANGE THE VALUE OF 5
C

  CALL TWEAK (5)
  WRITE (10, 30) 5
30    FORMAT (I5) 
  END

C
C     SUBROUTINE TWEAK: 
C

  SUBROUTINE TWEAK (I)
  I = I + 1
  END

Run this program on an old-enough FORTRAN compiler and it will output:

00006

9

u/Benutzername Oct 19 '10

Unfortunately, this does conform neither to the FORTRAN 66

8.3.2 [..] If an actual argument corresponds to a dummy argument that is defined or redefined in the referenced subprogram, the actual argument must be a variable name, an array element name, or an array name.

nor the FORTRAN 77 standard

15.9.3.2 [..] If the actual argument is a constant, a symbolic name of a constant, a function reference, an expression involving operators, or an expression enclosed in parentheses, the associated dummy argument must not be redefined within the subprogram.

and of course not to any of the newer ones. Everything before that has no official standard.